Hibernate:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

时间:2014-04-04 12:06:19

标签: java hibernate

当我尝试使用hibernate + postgresql

获取List时收到以下错误

为MyObject:

@Entity
@Table(name = "my_objects", schema = "public")
public class MyObject implements java.io.Serializable {

private static final long serialVersionUID = 1L;
@Id
@Column(name = "my_object_id", unique = true, nullable = false)
@GeneratedValue
private long myObjectId;

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "owner_id")
private User user;

@Column(name = "scheme_zone_name", length = 150)
private String schemeZoneName;

@Temporal(TemporalType.DATE)
@Column(name = "create_dt", length = 13)
private Date createDt;

@Column(name = "comment", length = 250)
private String comment;


public MyObject() {
}

public MyObject(long myObjectId) {
    this.myObjectId = myObjectId;
}

public MyObject(long myObjectId, User user,  String myObjectName, Date createDt, String) {
    this.schemeZoneId = schemeZoneId;
    this.user = user;
    this.myObjectName = myObjectName;
    this.createDt = createDt;
    this.comment = comment;
} ....getters/setters .....}

用户:

@Entity
@Table(name = "dp_users", schema = "public")
public class User implements java.io.Serializable {

private static final long serialVersionUID = 1L;
@Id
@Column(name = "user_id", unique = true, nullable = false, precision = 5, scale = 0)
@GeneratedValue
private int userId;

@Column(name = "user_login", nullable = false, length = 25)
private String userLogin;

@Column(name = "user_password", nullable = false, length = 25)
private String userPassword;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
private Set<MyObject> myObjects = new HashSet(0);

public User() {
}

public User(int userId, String userLogin, String userPassword) {
    this.userId = userId;
    this.userLogin = userLogin;
    this.userPassword = userPassword;
}

public User(int userId, String userLogin, String userPassword, Set<MyObject> myObjects) {
    this.userId = userId;
    this.userLogin = userLogin;
    this.userPassword = userPassword;
    this.myObjects = myObjects;
} ....getters/setters .....}

首先,我获取用户并将其保存在用户对象中:

public User login(String login, String password) {
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        return (User) session.createCriteria(User.class)
                .add(Restrictions.eq("userLogin", login).ignoreCase())
                .add(Restrictions.eq("userPassword", password)).uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return null;
}

然后,当我尝试按用户获取MyObject列表时:

public List<MyObject> getMyObjects(User user) throws SQLException {
    List<MyObject> o = null;
    Session session = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        o = session.createCriteria(MyObject.class)
                .add(Restrictions.eq("user", user))
                .addOrder(Order.asc("myObjectName"))
                .list();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return o;
}

我收到了

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cc.testProject.common.User

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该使用相同的会话来检索所有实体。会话范围必须与您的工作单元匹配,不要为每个操作实例化它。

请参阅Struggling to understand EntityManager proper use