我正在使用Spring集成Hibernate项目。但是我在项目中添加了一对多,我收到了错误
我的SQL脚本:
create table users(
username varchar(50) not null primary key,
password varchar(50) not null,
enabled boolean not null
);
create table authorities (
username varchar(50) not null,
authority varchar(50) not null,
constraint fk_authorities_users foreign key(username)references users(username));
UserVO类地图表用户
@Entity
@Table(name="users",schema="public")
public class UserVO implements UserDetails,Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private boolean enabled;
private Set<AuthorityVO>authorityList = new HashSet<AuthorityVO>();
@Id
@Column(name="username",length=50,nullable=false)
@Override
public String getUsername() {
// TODO Auto-generated method stub
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="password",length=50,nullable=false)
@Override
public String getPassword() {
// TODO Auto-generated method stub
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="enabled",nullable=false)
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL,mappedBy="user")
public Set<AuthorityVO> getAuthorityList() {
return authorityList;
}
public void setAuthorityList(Set<AuthorityVO> authorityList) {
this.authorityList = authorityList;
}
@Transient
@Override
public Collection<GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return null;
}
@Transient
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return false;
}
@Transient
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return false;
}
@Transient
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return false;
}
}
AuthorityVO类地图表权限
@Entity
@Table(name="authorities",schema="public")
public class AuthorityVO {
private UserVO user;
private String authority;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="username",nullable=false)
@ForeignKey(name="fk_authorities_users")
public UserVO getUser() {
return user;
}
public void setUser(UserVO user) {
this.user = user;
}
@Column(name="authority",length = 50,nullable=false)
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
起初我没有在UserVO中添加@OneToMany而不是在AuthorityVO中添加,一切都很好。但是我将这些东西添加到我的项目中我得到了错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.vaannila.vo.AuthorityVO
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
我知道错误意味着AuthorityVO不是真正的地图与表权限。但我找到了很多网络和电子书,但我找不到问题在哪里修复。你能帮帮我吗?
答案 0 :(得分:2)
你的实体AuthorityVO没有主键(数据库),而且在hibernate中也没有ID-Column(用@Id注释)。
此外,最好在UserVO中使用良好的数据库设计和语义列中的一些数字主键。
答案 1 :(得分:0)
AnnotationException:没有为实体指定标识符:com.vaannila.vo.AuthorityVO
它意味着您没有任何数据库中的AuthorityVO类主键 没有ID列像@id map with database。 因此,对于AuthorityVO类,Hibernate找不到任何标识符(用于区分b / w对象并用于从数据库中获取对象的唯一列)。