我有两个模型类,User和Salutation如下。用户表中的外键(salutation_id)是可选的。合并后立即在会话对象上刷新时保存用户对象,我得到下面的错误。对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.example.model.Salutation
对象引用一个未保存的瞬态的实例 - 冲洗之前保存的瞬态的实例:com.example.model.Salutation
数据表称谓已被插入独立用户表的。当我试图保存具有salutation_id的用户对象为空时,它会抛出此错误。
任何帮助将是可观的。
@Entity
@Table(name = "user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(unique = true, nullable = false)
private String userName;
@ManyToOne
@JoinColumn(name = "salutation_id")
private Salutation salutationRef;
//Getter and Setter
}
@Entity
@Table(name = "salutation")
public class Salutation implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@ElementCollection
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "salutationRef")
private List<Salutation> salutations;
//Getter and Setter
}
public Object create(Object object) {
Session session = sessionFactory.getCurrentSession();
Transaction txn = session.beginTransaction();
try {
object = (Object) session.merge(object);
session.flush(); //Getting error at this line object references an unsaved transient instance - save the transient instance before flushing: com.example.model.Salutation
txn.commit();
} catch (Exception e) {
txn.rollback();
e.printStackTrace();
}
return object;
}
感谢您的帮助。
感谢您的输入@Eugene。我很感激。 –