我是新来的hibernat,我有两个类,我试图将数据保存在Derby嵌入式数据库中,但是当我用外键保存类'produto'时从销售出现以下错误: 错误:JPA /休眠错误违反外键约束
Caused by: ERROR 23503: INSERT on table 'PRODUTOS' caused a violation of foreign key constraint 'FKIOY8UEWC87473SNMMUOPJKUVB' for key (236). The statement has been rolled back.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.execute.ForeignKeyRIChecker.doCheck(Unknown Source)
at org.apache.derby.impl.sql.execute.RISetChecker.doFKCheck(Unknown Source)
at org.apache.derby.impl.sql.execute.InsertResultSet.normalInsertCore(Unknown Source)
at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
... 82 more
类文达:
@Entity
@Table(name = "Venda")
@Access(value = AccessType.PROPERTY)
public class Venda {
@Id
@GeneratedValue
@Column(name = "venda_id")
public Long getId() {
return id;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "produtos_id")
public List<Produtos> getProdutos() {
return produtos;
}
public void setProdutos(List<Produtos> produtos) {
this.produtos = produtos;
}
类Produtos:
@Entity
@Table(name = "Produtos")
@Access(value = AccessType.PROPERTY)
public class Produtos {
@Id
@GeneratedValue
@Column(name = "produtos_id")
public Long getId() {
return id;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "venda_id", nullable = false)
public Venda getVenda() {
return venda;
}
节能:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("cliente");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(venda);
for (Produtos produto : produtos) {
produto.setVenda(venda);
}
for (Produtos produto : produtos) {
manager.persist(produto);
}
manager.getTransaction().commit();
manager.close();
factory.close();
请有人能帮助我吗?
我改变了代码,并没有工作,然后删除银行,并运行它的工作后,谢谢你 – user3576248