gpt4 book ai didi

jpa - 保持一对多的关系

转载 作者:行者123 更新时间:2023-12-04 06:51:09 27 4
gpt4 key购买 nike

我正在使用 Java EE 6。只是想先把它扔掉
这是关系。一个 customer可以有多个 facility这是客户类

@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="CUSTOMER_FK", nullable=false)
private List<Facility> facilities;

public Customer(){
facilities = new ArrayList<Facility>();
}

public Customer(Long id, String name, List<Facility> facilities) {
this.id = id;
this.name = name;
this.facilities = facilities;
}
//set and get method
...

public void addFacility(Facility facility){
this.facilities.add(facility);
}
}

这里是设施类
@Entity
public class Facility {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;

public Facility(){}

public Facility(Long id, String name, Address address, List<Project> project) {
this.id = id;
this.name = name;
}

//Set and get method
...
}

现在主要是如果我只有这个,那么它工作正常。我可以看到这被插入到我的数据库中
Customer customer = new Customer();
customer.setName("Wake Forest University");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EntityLibraryPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(customer);
tx.commit();

但是,一旦我尝试添加设施并强制 FK 约束。事情开始破裂。
Customer customer = new Customer();
customer.setName("Wake Forest University");
Facility facility = new Facility();
facility.setName("Xia");
customer.addFacility(facility);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EntityLibraryPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(customer);
tx.commit();

这是错误。首先,我将相同的“维克森林大学”客户插入到 CUSTOMER 表中(不是 name 属性未设置为唯一,而是一次将两个条目插入到数据库中)。但是,没有任何东西可以插入到我的 FACILITY 表中。知道为什么吗?错误代码之一是 Field 'customer_fk' doesn't have a default value .也许这可以暗示你们一点。我不知道。当我查看错误日志时: Call: INSERT INTO FACILITY (ID, NAME, STREET2, STREET1, ZIPCODE, STATE, CITY, COUNTRY) VALUES (?, ?, ?, ?, ?, ?, ?, ?)bind => [2, Xia, null, null, null, null, null, null] , 我的 FACILITY表,有一个字段 CUSTOMER_FK ,这是当我试图在 CUSTOMER 之间强制一对多关系时创建的和 FACILITY ,但查询永远不会在该字段中插入任​​何内容

最佳答案

First I get double of the same "Wake Forest University" customer insert into CUSTOMER table



好吧,您没有在 name 上定义任何唯一性约束。的 Customer实体所以没有什么可以阻止这一点(尽管两个记录都有不同的 PK)。如果您想强制执行 name 的唯一性,设置唯一约束:
@Column(unique=true)
String name;

One of the error code is Field 'customer_fk' doesn't have a default value.



我认为注释行是“有罪的”:
tx.begin();
em.persist(customer);
//em.persist(facility); //DON'T DO THIS
tx.commit();

首先,该设施对其客户一无所知(这是一个单向关联,从 CustomerFacitlity ),因此 JPA 无法设置 FK,因此出现错误消息。

其次,因为你是级联 全部 来自 Customer 的操作至 Facility (使用 cascade=CascadeType.ALL 注释中的 @OneToMany),您不需要 persist facility实例。

所以,不要在 facility 上调用 persist例如,让 JPA 从 customer 级联事物.

关于jpa - 保持一对多的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3112570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com