简体   繁体   English

Hibernate / JPA使用生成的UUID持久保存OneToMany对象

[英]Hibernate/JPA persist OneToMany objects with generated UUID's

I have been using Hibernate a lot but using Hibernate/JPA with UUID got me stumped a bit. 我一直在使用Hibernate,但是将Hibernate / JPA与UUID结合使用会使我有些困惑。 I am using hibernate 5.2.12.Final. 我正在使用休眠5.2.12.Final。

I have an object called TimePeriod with this mapping: 我有一个名为TimePeriod的对象,带有此映射:

@Entity(name = "time_period")    
public class TimePeriod extends AbstractDomainObject {

   @OneToMany(mappedBy = "timePeriod", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private List<TimePeriodBlock> timePeriodBlocks = new ArrayList<>();
   ...
   public void addTimePeriodBlock(TimePeriodBlock timePeriodBlock) {
      timePeriodBlock.setTimePeriod(this);
      this.timePeriodBlocks.add(timePeriodBlock);
   }
   ...

With the following child relationship: 具有以下子关系:

@Entity(name = "time_period_block")
public class TimePeriodBlock extends AbstractDomainObject {
   ...   
   @ManyToOne
   @JoinColumn(name = "time_period_id", nullable = false)
   private TimePeriod timePeriod;
   ...

They share this super class: 他们共享这个超级类:

@MappedSuperclass
public abstract class AbstractDomainObject {
   ...
   @Id
   @GeneratedValue
   @Column(name = "id", columnDefinition = "uuid", updatable = false)
   private UUID id;
   ...

When I execute the following: 当我执行以下命令时:

// pseudo code
TimePeriod t = new TimePeriod();
t.setName("test");
TimePeriodBlock b = new TimePeriodBlock();
t.addTimePeriodBlock(b);

em.persist(t);

I get the exception: 我得到例外:

...
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : test.TimePeriodBlock.timePeriod
...

Some notes: 一些注意事项:

I strongly believe that this could be because Hibernate generates the UUID (and not the database) but, since I am not sure, I hope some fellow Developer might know how this could work. 我坚信这可能是因为Hibernate生成了UUID(而不是数据库),但是,由于不确定,我希望其他开发人员可以知道这是如何工作的。

I am using PostgreSQL 9.6 and the database can also generates UUIDv4 but requires compiling an extra extension so I opted for Hibernate to generate it. 我使用的是PostgreSQL 9.6,数据库也可以生成UUIDv4,但需要编译一个额外的扩展,因此我选择了Hibernate来生成它。

When I enter some data in the database and retrieve the data it is fetched without any error. 当我在数据库中输入一些数据并检索数据时,将其正确读取。

Storing other objects without @ManyToOne relationships do store without any error and have a UUID that is generated by Hibernate. 存储没有@ManyToOne关系的其他对象的确存储没有任何错误,并且具有由Hibernate生成的UUID。

Well after some debugging and using Luay Abdulreheem suggestion I found out that hibernate is working just fine; 在进行一些调试并使用Luay Abdulreheem的建议之后,我发现休眠状态可以正常工作。 in this case my objects are send using a REST interface (using Jackson) and the reference to the parent was lost as the unmarshalling of the JSON is done using fields. 在这种情况下,我的对象是使用REST接口发送的(使用Jackson),并且由于使用字段完成JSON的编组,所以丢失了对父对象的引用。

So nothing to see here, move along... 所以没什么可看的,继续前进...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM