当实体保存时,抛出与实体ID相关的错误。我试图将XML映射中的生成器类更改为本机,但在这种情况下,Hibernate根本不保存实体。
我使用PostgreSQL。
org.hibernate.AssertionFailure: null identifier
org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:69)
org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:240)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:306)
这是我的XML mappaing。
<hibernate-mapping>
<class name="com.mello.core.domain.model.board.Board" table="board">
<id name="id" type="java.lang.Long" column="id">
<generator class="identity"/>
</id>
<many-to-one name="profile" cascade="merge" column="id_profile"
class="com.mello.core.domain.model.account.Profile" unique="true"/>
<property name="name" column="board_name" type="java.lang.String"/>
<property name="description" column="description" type="java.lang.String"/>
<property name="important" column="important" type="java.lang.Boolean"/>
</class>
<hibernate-mapping>
<joined-subclass name="com.mello.core.domain.model.account.Profile" extends="com.mello.core.domain.model.account.User"
table="profile">
<key column="fk_user_id"/>
<bag name="friends" lazy="false" table="friend"
cascade="all" inverse="false" fetch="select">
<key>
<column name="id" not-null="false"/>
</key>
<many-to-many entity-name="com.mello.core.domain.model.account.Profile">
<column name="friend_id"/>
</many-to-many>
</bag>
</joined-subclass>
public boolean save(Board board) {
Session session = HibernateUtil.getSession(sessionFactory);
session.beginTransaction();
try {
session.merge(board);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
throw new RepositoryException(e);
} finally {
HibernateUtil.closeSession(session);
}
return true;
}