我試圖堅持以下幾點:堅持@OneToMany名單
@Entity
@Table(name="tb_father")
public class Father implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "tb_father_id_seq", sequenceName = "tb_father_id_seq")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "tb_father_id_seq")
@Column(name = "id", nullable = false)
private Integer id;
@OneToMany(fetch = FetchType.LAZY, mappedBy="father", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Son> sons;
... Gets and Sets
}
@Entity
@Table(name="tb_son")
public class Son implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "tb_son_id_seq", sequenceName = "tb_son_id_seq")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "tb_son_id_seq")
@Column(name = "id", nullable = false)
private Integer id;
@Inject
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="father", referencedColumnName="id")
private Father father;
... Gets and Sets
}
例子:
Father father = new Father();
Son son = new Son();
...
father.getSons().add(son);
在我FatherDAO
@Stateless
public class FatherDAO {
@PersistenceContext(unitName = "test")
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void persistFatherSons(Father father) throws ApplicationException {
try {
em.persist(father);
} catch(Exception e) {
...
}
}
...
}
表:
CREATE TABLE tb_father
(
id integer NOT NULL,
CONSTRAINT tb_father_pkey PRIMARY KEY (id),
)
CREATE TABLE tb_son
(
id integer NOT NULL,
father integer,
CONSTRAINT tb_son_pkey PRIMARY KEY (id),
CONSTRAINT fk41b1efcfa0ebdd8c FOREIGN KEY (father)
REFERENCES tb_father (id) MATCH SIMPLE
)
運行「em.persist(父)」時,只有父親保存在數據庫中,而不是兒子(列表)。現在
錯誤:
08:00:21,123 WARN [com.arjuna.ats.arjuna] (http--10.36.1.49-8180-1) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff7f000101:-81b794d:5523b2cf:61, org.hibernate.engine[email protected]6bb7796b >: javax.persistence.PersistenceException: error during managed flush
at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1486) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
...
就緒!這兩個實體ai是我創建的示例,用於演示此處正在發生的問題。該示例中缺少的是Float字段。該字段爲@ Size並且這是導致錯誤的原因。
Thankssss
我解決不了...但:( – Mamga