1
@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)) ^ ^ | | 德

我需要实体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
4

0 回答 0