jpa - 持久化一对多关系

标签 jpa jakarta-ee persistence entity-relationship object-relational-model

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

@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
     ...
}

现在在 main 中,如果我只有这个,那么它就可以正常工作。我可以看到它被插入到我的数据库中

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();

这是错误。首先,我将同一“Wake Forest University”客户的两倍插入到 CUSTOMER 表中(不是 name 属性未设置为唯一,而是它一次将两个条目插入数据库)。但是,没有任何内容插入到我的 FACILITY 表中。知道为什么吗?错误代码之一是字段“customer_fk”没有默认值。也许这可以给你们一点暗示。我不知道。当我查看错误日志时:调用:INSERT INTO FACILITY (ID, NAME, STREET2, STREET1, ZIPCODE, STATE, CITY, COUNTRY) VALUES (?, ?, ?, ?, ?, ?, ?, ? )bind => [2, Xia, null, null, null, null, null, null],我的 FACILITY 表,有一个字段 CUSTOMER_FK,其中当我尝试强制 CUSTOMERFACILITY 之间建立一对多关系时创建,但查询从未在该字段中插入任​​何内容

最佳答案

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

好吧,您没有对 Customer 实体的 name 定义任何唯一性约束,因此没有什么可以阻止这种情况(尽管两条记录具有不同的 PK)。如果您想强制名称的唯一性,请设置唯一约束:

@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/

相关文章:

java - 如何实现 CrudRepository 的自定义方法?

java - 为每个子类创建单独的 hibernate 搜索索引

postgresql - Glassfish 2.1 从不重用 Postgresql 和 Eclipselink 的语句?

java - Collections.sort 不是排序

Python 持久套接字连接

java - 面临 JPA 合并问题。合并操作未反射(reflect)在数据库中并且查询执行了两次

spring - 可以从 SQL 脚本引用另一个 SQL 文件

java - 换一个新版本的JDK?

object - D 中的自动数据持久化

java - JBoss7 + PostgreSQL 新的缺失/未满足的依赖关系