簡體   English   中英

休眠:對象引用了一個未保存的瞬態實例-在刷新之前保存該瞬態實例

[英]Hibernate : object references an unsaved transient instance - save the transient instance before flushing

當我嘗試使用休眠+ PostgreSQL獲取列表時收到以下錯誤

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 .....}

首先,我得到User並將其保存在User對象中:

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

我做錯了什么?

您應該使用相同的會話來檢索所有實體。 會話范圍必須與您的工作單元匹配,請勿為每個操作實例化它。

請參閱努力了解EntityManager的正確使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM