Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA:防止级联操作[持久化,删除…]_Java_Hibernate_Jpa - Fatal编程技术网

Java JPA:防止级联操作[持久化,删除…]

Java JPA:防止级联操作[持久化,删除…],java,hibernate,jpa,Java,Hibernate,Jpa,我有两个实体 @Entity @Table(name="parent") public class Parent { @Id String uuid; @ElementCollection(fetch=FetchType.EAGER) @CollectionTable( name="child", joinColumns=@JoinColumn(name="parent_uuid", insertable=false, updatable=false)

我有两个实体

@Entity
@Table(name="parent")
public class Parent {
  @Id
  String uuid;

  @ElementCollection(fetch=FetchType.EAGER)
  @CollectionTable(
      name="child",
      joinColumns=@JoinColumn(name="parent_uuid", insertable=false, updatable=false)
  )

  @Column(name="uuid")
  private Set<String> childrenUuids = new HashSet<String>();
}

@Entity
@Table(name="child") 
public class Child {
  @Id
  String uuid;

  @Column(name="parent_uuid")
  String parentUuid;

}
@实体
@表(name=“parent”)
公共类父类{
@身份证
字符串uuid;
@ElementCollection(fetch=FetchType.EAGER)
@收集表(
name=“child”,
joinColumns=@JoinColumn(name=“parent_uuid”,insertable=false,updateable=false)
)
@列(name=“uuid”)
private Set childrenUuids=new HashSet();
}
@实体
@表(name=“child”)
公营儿童{
@身份证
字符串uuid;
@列(name=“parent_uuid”)
字符串parentUuid;
}

现在,当我持久化Parent时,childrenUuids中的子对象会自动持久化,因为多通关系。我想防止所有到父级的操作(例如,持久化、删除…)被级联到子级,JPA是否可以?我已经研究了几天,但没有找到答案。谢谢。

您应该使用@OneToMany而不是@ElementCollection。默认情况下,@OneToMany不会级联。据我所知,@ElementCollection总是级联的,这是有意义的,因为“@ElementCollection定义了一个基本类型或可嵌入类的实例集合”,基本类型/可嵌入类被视为其父类的一个组成部分。

@ElementCollection总是级联的。我最终通过实现@ElementCollection的解决方案来解决这个问题。我仍然使用JPA注释,取而代之的是,我在@ElementCollection上方添加@Transient,使JPA忽略它。然后,我将我的实现作为JPA加载后监听器放入每个实体,在加载实体后,它将填充每个集合。

谢谢。问题是我的子对象在实际情况下相当大,实际上我只需要它的一列。加载整个子对象对我来说太贵了。有没有办法在没有级联的情况下实现CollectionTable的相同效果?显然@OneToMany用于关系,而集合不是关系,所以不应该使用它。