One-to-many relationship Hibernate Problem
I'm having Spring intergrate Hibernate project.But I add one-to-many in project I got error
My SQL Script:
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(us开发者_StackOverflow社区ername));
UserVO class map table User
@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 class map table authorities
@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;
}
At first I didn't add @OneToMany in UserVO and not AuthorityVO,All was fine.But i add those things to my project I got error
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)
I know error means AuthorityVO not true map with Table authorities.But I find a lot of web and ebooks but I cannnot find where is problem to fix.Can you help me?
Your entity AuthorityVO has no primary key (database) and there fore also no ID-Column (annotated with @Id) in hibernate.
Further more it is better to use always some numeric primary keys in a good database design and a semantic column as you did with UserVO.
AnnotationException: No identifier specified for entity: com.vaannila.vo.AuthorityVO
it's mean you are not have any primary key for AuthorityVO class in database and no ID column like @id map with database. So Hibernate can not find any identifier( unique column that is use to differtiate b/w objects and use to fetch objects from database ) for AuthorityVO class.
精彩评论