问题 插入后,我可以在我的客户和地址表中找到我的数据。但我无法在我的配置表中找到任何数据:Customers_Addresses
代码
客户实体
@Entity @Table(姓名=" CUSTOMERS") 公共类Customer实现Serializable {
/**
*
*/
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 =&#34; ADDRESSES&#34;) public class Address实现Serializable {
/**
*
*/
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 assotiation表的脚本:Customers_Addresses
创建表CUSTOMERS_ADDRESSES( IDCUSTOMER int不为null, IDADDRESS int不为null, 主键(IDCUSTOMER,IDADDRESS), key fk_cust(IDCUSTOMER), key fk_add(IDADDRESS), 约束fk_cust外键(IDCUSTOMER)引用客户(IDCUSTOMER), 约束fk_add外键(IDADDRESS)引用地址(IDADDRESS)
答案 0 :(得分:0)
您在连接表中看不到任何信息的主要原因是您在添加连接表数据之前保存记录。请在此处查看您的代码:
customerService.insertCustomer(customer);
customer.getAddresses().add(address);
customer.getAddresses().add(address1);
应改为:
customer.getAddresses().add(address);
customer.getAddresses().add(address1);
customerService.insertCustomer(customer);
同样,您的代码也会对您的customer1
实例产生同样的问题。