Hey,
I'm trying to add a simple reference to a Unit class from Appfuses User entity (Spring MVC Basic) but keep getting a org.hibernate.AnnotationException. I've used the @Entity tag in my class, added it to the hibernate.cfg.xml file, and tried adding my class to the persistance.xml file, but with no help. When I look at the generated User table in the db, I can see that the unit_id field was added successfully, but the mvn tests/unit tests always give me the same problem. Any ideas why?
/U
Hibernate version:
hibernate-annotations-3.3.0.ga
User.java
Code:
package se.blabla.goals.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
* This class represents the basic "user" object in AppFuse that allows for authentication
* and user management. It implements Acegi Security's UserDetails interface.
*
* @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
* Updated by Dan Kibler (dan@getrolling.com)
* Extended to implement Acegi UserDetails interface
* by David Carter david@carter.net
*/
@Entity
@Table(name="app_user")
public class User extends BaseObject implements Serializable, UserDetails {
private static final long serialVersionUID = 3832626162173359411L;
private Long id;
private String username; // required
private String password; // required
private String confirmPassword;
private String passwordHint;
private String firstName; // required
private String lastName; // required
private String email; // required; unique
private String phoneNumber;
private Integer version;
private Set<Role> roles = new HashSet<Role>();
private boolean enabled;
private boolean accountExpired;
private boolean accountLocked;
private boolean credentialsExpired;
private Unit unit;
public User() {}
public User(final String username) {
this.username = username;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
@Column(nullable=false,length=50,unique=true)
public String getUsername() {
return username;
}
@Column(nullable=false)
public String getPassword() {
return password;
}
@Transient
public String getConfirmPassword() {
return confirmPassword;
}
@Column(name="password_hint")
public String getPasswordHint() {
return passwordHint;
}
@Column(name="first_name",nullable=false,length=50)
public String getFirstName() {
return firstName;
}
@Column(name="last_name",nullable=false,length=50)
public String getLastName() {
return lastName;
}
@Column(nullable=false,unique=true)
public String getEmail() {
return email;
}
@Column(name="phone_number")
public String getPhoneNumber() {
return phoneNumber;
}
@Transient
public String getFullName() {
return firstName + ' ' + lastName;
}
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name="user_role",
joinColumns = { @JoinColumn( name="user_id") },
inverseJoinColumns = @JoinColumn( name="role_id")
)
public Set<Role> getRoles() {
return roles;
}
@Transient
public List<LabelValue> getRoleList() {
List<LabelValue> userRoles = new ArrayList<LabelValue>();
if (this.roles != null) {
for (Role role : roles) {
// convert the user's roles to LabelValue Objects
userRoles.add(new LabelValue(role.getName(), role.getName()));
}
}
return userRoles;
}
public void addRole(Role role) {
getRoles().add(role);
}
@Transient
public GrantedAuthority[] getAuthorities() {
return roles.toArray(new GrantedAuthority[0]);
}
@Version
public Integer getVersion() {
return version;
}
@Column(name="account_enabled")
public boolean isEnabled() {
return enabled;
}
@Column(name="account_expired",nullable=false)
public boolean isAccountExpired() {
return accountExpired;
}
@Transient
public boolean isAccountNonExpired() {
return !isAccountExpired();
}
@Column(name="account_locked",nullable=false)
public boolean isAccountLocked() {
return accountLocked;
}
@Transient
public boolean isAccountNonLocked() {
return !isAccountLocked();
}
@Column(name="credentials_expired",nullable=false)
public boolean isCredentialsExpired() {
return credentialsExpired;
}
@Transient
public boolean isCredentialsNonExpired() {
return !credentialsExpired;
}
@ManyToOne
public Unit getUnit() {
return unit;
}
//...more stuff here
}
Unit.javaCode:
package se.blabla.goals.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@Entity
public class Unit extends BaseObject {
private static final long serialVersionUID = -8088015482852305474L;
private Long id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//...more stuff here
}
hibernate.cfg.xml:Code:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="se.blabla.goals.model.Unit" />
<mapping class="se.blabla.goals.model.User" />
<mapping class="se.blabla.goals.model.Role" />
<mapping class="se.blabla.goals.model.Category" />
<mapping class="se.blabla.goals.model.Goal" />
</session-factory>
</hibernate-configuration>
Stack trace:Code:
-------------------------------------------------------------------------------
Test set: se.blabla.goals.dao.hibernate.HibernateConfigurationTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.031 sec <<< FAILURE!
testColumnMapping(se.blabla.goals.dao.hibernate.HibernateConfigurationTest) Time elapsed: 0.015 sec <<< ERROR!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on se.blabla.goals.model.User.unit references an unknown entity: se.blabla.goals.model.Unit
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByName(AbstractAutowireCapableBeanFactory.java:1061)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1009)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:291)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:230)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:195)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:102)
at junit.framework.TestCase.runBare(TestCase.java:132)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on se.blabla.goals.model.User.unit references an unknown entity: se.blabla.goals.model.Unit
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:474)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:295)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:753)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:691)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1390)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1359)
... 33 more
Name and version of the database:
Mysql Server 5.0