简体   繁体   English

以多对一关系归档的版本导致对象引用未保存的瞬态实例

[英]Version filed in many-to-one relation causes object references an unsaved transient instance

i have a class like this :我有一个这样的课程:

public class User(){
        private String name;
        private Integer version ; 

        //getter & setter 
}

and the hibernate mapping file is :并且休眠映射文件是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.model.User" table="User">
        <id name="id" column="Id" type="java.lang.Integer">
            <generator class="sequence" >
                <param name="sequence">SEQ_User</param>   
            </generator>
        </id>
        <version name="version" column="version" type="Integer" />
        <property name="name"    column="Name"   type="string" not-null="true"  />
 </class>

</hibernate-mapping>

when i use as a many-to-one relation like this :当我使用这样的多对一关系时:

public class UserDetail(){
        private String tel;
        private User    user;
   // getter & setter 
    }

and this is UserDetail.hbm.xml file :这是 UserDetail.hbm.xml 文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.model.UserDetail" table="User_Detail">
        <id name="id" column="Id" type="java.lang.Integer">
            <generator class="sequence" >
                <param name="sequence">SEQ_User_Detail</param>   
            </generator>
        </id>

        <property name="tel"    column="tel"   type="string" not-null="true"  />
        <many-to-one name="user"            column="user_Id"        entity-name="org.model.User" />

 </class>

</hibernate-mapping>

when i want to save the UserDetail take this exception :当我想保存 UserDetail 时,请采取以下例外:

object references an unsaved transient instance - save the transient instance before flushing对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

i save the UserDetail object in the way below :我以下面的方式保存 UserDetail 对象:

@Override
    public void add(UserDetail entity) {
        Session session = getSession();
        session.save(entity);
    }

and i set User in the UserDetail like this :我在 UserDetail 中设置 User 如下:

entity.setUser(new User(1));

i just know the UserId and never load the user object.我只知道 UserId 并且从不加载用户对象。

Although you haven't added the code for UserDetails and the persistence logic, I suppose you have a @ManyToOne association to a User in UserDetails .尽管您尚未添加UserDetails和持久性逻辑的代码,但我想您在UserDetails中与User有一个@ManyToOne关联。

If you have a User and a UserDetails that you want to persist, you have to call persist() on both entities.如果您有想要保留的UserUserDetails ,则必须在两个实体上调用 persist() 。 Unless you have a parent-side association in User , you cannot use cascading since cascade makes sense only from parent to children but not vice versa .除非您在User有父端关联,否则您不能使用级联,因为级联仅从父级到子级才有意义,反之亦然

This has nothing to do with concurrency or versioning as you initially suspected.正如您最初怀疑的那样,这与并发或版本控制无关。

You said that:你说:

I don't have any problem when i remove version field我删除版本字段时没有任何问题

That could be explained if you set an id to a transient User instance and you try to call persist() :如果您将 id 设置为瞬态User实例并尝试调用persist()则可以解释这一点:

User user = new User();
user.setId(1L);
user.setVersion(0);
session.persist(user);

Because you have a sequence identifier generator, it is Hibernate that should assign identifiers.因为您有一个序列标识符生成器,​​所以应该分配标识符的是 Hibernate。 Also, the version property should never be assigned manually.此外,永远不应手动分配版本属性。 If you have a detached entity, you need to call merge instead of persist .如果您有一个分离的实体,则需要调用 merge 而不是 persist

Update更新

According to your update and the new source code, I can't find any particular issue.根据您的更新和新的源代码,我找不到任何特定问题。 However, you should know that the Session might have been opened before and maybe there are other entities that you added previously to creating the User entity.但是,您应该知道Session之前可能已经打开,并且可能您之前添加了其他实体来创建User实体。 It's only during the flush that entity state transitions are propagated to the DB.只有在刷新期间实体状态转换才会传播到数据库。

在我调试后,如果我们将版本字段 Null 放在 User 中,我会收到异常,但是当我将其设置为零时,问题就解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM