单向多对一CascadeType.ALL

时间:2013-07-08 10:10:11

标签: java jpa one-to-many openjpa many-to-one

@Table(name="A")
public class A implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @OneToMany(mappedBy = "b", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   private List<B> bList;

  //getter and setters
}

@Table(name="B")
public class B implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
   private A a;

   @OneToMany(mappedBy = "c", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private List<C> cList;

  //getter and setters
}

@Table(name="C")
public class C implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @ManyToOne(fetch = FetchType.LAZY)
   private B b;

   @OneToMany(mappedBy = "d", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private List<D> dList;

  //getter and setters
}

@Table(name="D")
public class D implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @ManyToOne(fetch = FetchType.LAZY)
   private C c;

  //getter and setters
}

@Table(name="E")
public class E implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private C c;

  //getter and setters
}

A(双向B)    ^    |    B(双向A和C)    ^    |    C((双向B和D)和(单向E)) ^ ^ | | D E

我需要一年内实体E的数据。(因此单向连接)

类是双向连接或单向连接。我保存实体A并得到以下例外:

<openjpa-2.1.2-SNAPSHOT-r422266:1384519 fatal user error> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged object "com.xxx.yyy.data.entity.C@18e0a217" in life cycle state unmanaged while cascading persistence via field "com.xxx.yyy.data.entity.E.c" during flush. However, this field does not allow cascade persist. You cannot flush unmanaged objects or graphs that have persistent associations to unmanaged objects. Suggested actions: a) Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or "all" (JPA orm.xml), b) enable cascade-persist globally, c) manually persist the related field value prior to flushing. d) if the reference belongs to another context, allow reference to it by setting StoreContext.setAllowReferenceToSiblingContext(). FailedObject: com.xxx.yyy.data.entity.C@18e0a217

0 个答案:

没有答案