[英]Hibernate 4 Entity Mapping with Spring
I've got 2 entity classes as follows: 我有2个实体类,如下所示:
@Entity
@Table(name = "USER_ACCT")
public class UserAccount implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_ACCT_ID_SEQ")
@SequenceGenerator(name="USER_ACCT_ID_SEQ", sequenceName="USER_ACCT_ID_SEQ")
@Column(name = "USER_ACCT_ID")
protected Long id;
@Basic(optional = false)
@Column(length = 50, unique = true)
private String username;
@Basic(optional = false)
@Column(length = 128, nullable = false)
private String password;
@ManyToMany(targetEntity = UserPermission.class)
@JoinTable(name = "USER_ACCT_PERM",
joinColumns = @JoinColumn(name = "USER_ACCT_ID"),
inverseJoinColumns = @JoinColumn(name = "USER_PERM_ID", referencedColumnName = "USER_PERM_ID"))
private Set<UserPermission> permissions;
// getters and setters...
}
AND 和
@Entity
@Table(name = "USER_PERM")
public class UserPermission implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_PERM_ID_SEQ")
@SequenceGenerator(name="USER_PERM_ID_SEQ", sequenceName="USER_PERM_ID_SEQ")
@Column(name = "USER_PERM_ID")
protected Long id;
@Column(name = "PERM", unique = true, length = 255, nullable = false)
private String authority;
// getters and setters...
}
Now when I create an object of UserAccount and try to save it using the session factory it gives me the following error: 现在,当我创建UserAccount的对象并尝试使用会话工厂保存它时,出现以下错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rpm.domain.UserPermission
Shouldn't the UserPermission class be saved by itself?? UserPermission类不应该自己保存吗? This is the code that is used to persist:
这是用于持久化的代码:
@Transactional
public void addUser(){
Set<UserPermission> permissions = new HashSet<UserPermission>();
permissions.add(new UserPermission("ROLE_ADMIN"););
sessionFactory.getCurrentSession().save(new UserAccount("admin", "d033e22ae348aeb5660fc2140aec35850c4da997", permissions));
}
除非您指定级联选项,否则不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.