【问题标题】:manyTomany relation cant fine foreign keys in assotiation tablemanyTomany 关系在关联表中找不到外键
【发布时间】:2018-05-30 12:23:16
【问题描述】:

问题 插入后,我可以在我的客户和地址表中找到我的数据。但我无法在我的关联表中找到任何数据:Customers_Addresses

代码

客户实体

@实体 @Table(name = "客户") 公共类客户实现可序列化{

/**
 * 
 */
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;
}

地址实体

@实体 @Table(name = "地址") 公共类地址实现可序列化{

/**
 * 
 */
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;
}

主要代码

公开课测试{ 公共静态无效主要(字符串[]参数){ // 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_Addresses

创建表 CUSTOMERS_ADDRESSES ( IDCUSTOMER int 不为空, IDADDRESS int 不为空, 主键(IDCUSTOMER,IDADDRESS), 键 fk_cust (IDCUSTOMER), 键 fk_add (IDADDRESS), 约束 fk_cust 外键 (IDCUSTOMER) 引用客户 (IDCUSTOMER), 约束 fk_add 外键(IDADDRESS)引用地址(IDADDRESS)

【问题讨论】:

    标签: mysql spring hibernate jpa


    【解决方案1】:

    您在连接表中看不到任何信息的主要原因是您在添加连接表数据之前保存了记录。在此处查看您的代码:

    customerService.insertCustomer(customer);
    
    customer.getAddresses().add(address);
    customer.getAddresses().add(address1);
    

    应该改为:

    customer.getAddresses().add(address);
    customer.getAddresses().add(address1);
    customerService.insertCustomer(customer);
    

    同样,您的代码与您的 customer1 实例也存在同样的问题。

    【讨论】:

    • 非常感谢您的关注。现在一切正常。 24小时思考。 @Naros thks ......
    猜你喜欢
    • 2011-11-11
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2014-11-15
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多