插入后的问题,我可以找到我的数据到我的客户和地址表。但我找不到任何数据到我的关联表:客户\地址
代码
客户实体
@entity@table(name=“customers”)公共类customer实现可序列化{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDCUSTOMER")
private Long idCustomer;
@Column(name = "NOM")
private String nom;
@Column(name = "PRENOM")
private String prenom;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "CUSTOMERS_ADDRESSES",
joinColumns = { @JoinColumn(name = "IDCUSTOMER")},
inverseJoinColumns = {@JoinColumn(name = "IDADDRESS")}
)
private List<Address> addresses = new ArrayList<>();
//constructor
public Customer() {
}
public Customer(Long idCustomer, String nom, String prenom){
this.idCustomer = idCustomer;
this.nom = nom;
this.prenom = prenom;
}
//getters and setters
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public Long getIdCustomer() {
return idCustomer;
}
public void setIdCustomer(Long idCustomer) {
this.idCustomer = idCustomer;
}
地址实体
@entity@table(name=“addresses”)公共类地址实现可序列化{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IDADDRESS")
private Long idAddress;
@Column(name = "CITY")
private String city;
@Column(name = "COUNTRY")
private String country;
@ManyToMany(mappedBy ="addresses" )
private List<Customer> customers = new ArrayList<>();
public Address() {
}
public Address(Long idAddress, String city, String country){
this.idAddress = idAddress;
this.city = city;
this.country = country;
}
//Getters and setters
public Long getIdAddress() {
return idAddress;
}
public void setIdAddress(Long idAddress) {
this.idAddress = idAddress;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
public List<Customer> getCustomers() {
return customers;
}
主代码
公共类测试{public static void main(string[]args){//todo自动生成的方法存根
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
AddressService addressService = (AddressService) context.getBean("addressServiceImpl");
CustomerService customerService = (CustomerService) context.getBean("customerServiceImpl");
Address address = new Address();
address.setCity("ddd");
address.setCountry("ddd");
addressService.insertAddress(address);
Address address1 = new Address();
address1.setCity("ddd");
address1.setCountry("ddd");
addressService.insertAddress(address1);
//customer have two addreses
Customer customer = new Customer();
customer.setNom("ddd");
customer.setPrenom("ddd");
customerService.insertCustomer(customer);
customer.getAddresses().add(address);
customer.getAddresses().add(address1);
//Customer 1 have 1 adresses
Customer customer1 = new Customer();
customer1.setNom("ddd");
customer1.setPrenom("ddd");
customerService.insertCustomer(customer1);
customer1.getAddresses().add(address1);
}
sql关联表的脚本:customers\u addresses
创建表customers\u addresses(idcustomer int not null,idaddress int not null,主键(idcustomer,idaddress),键fk\u cust(idcustomer),键fk\u add(idaddress),约束fk\u cust foreign key(idcustomer)references customers(idcustomer),约束fk\u add foreign key(idaddress)references address(idaddress)
1条答案
按热度按时间pxiryf3j1#
在联接表中看不到任何信息的主要原因是在添加联接表数据之前保存了记录。请在此处查看您的代码:
改为:
同样地,您的代码在您的
customer1
示例也一样。