我有一个实体要与另一个实体作为多对一关系中的依赖项进行持久化。在构造要持久化的实体实例时,依赖实体总是从 EntityManager 加载。但是,当我尝试持久化实体时,Hibernate 也会尝试插入依赖项,从而导致错误。谁能告诉我为什么会这样以及如何禁用它?
实体定义如下:
@Entity
@Table(name="postal_code")
public class PostalCode implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
PostalCodePK id;
@MapsId("country")
@ManyToOne
@JoinColumn(name="country")
private Country country;
public PostalCode(Country country, String code) {
...
}
...
}
依赖实体 Country 定义如下:
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="code_alpha2", columnDefinition="bpchar")
@Size(min=2, max=2)
private String codeAlpha2;
...
}
构造和持久化实体的相关代码是:
PostalCode postalCode = new PostalCode(em.find(Country.class, "DE"), "01234");
em.getTransaction().begin();
em.persist(postalCode);
em.flush();
em.getTransaction().commit();
当我在 PostalCode 实例上调用 persist 时的日志输出是:
Hibernate:
insert
into
Country
(code_alpha3, code_alpha2)
values
(?, ?)
Aug 05, 2013 11:40:59 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 23505
Aug 05, 2013 11:40:59 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: duplicate key value violates unique constraint "country_pkey"
Detail: Key (code_alpha2)=(DE) already exists.
提前致谢!