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?
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:
[email protected]">Matt Raible</a>
* Updated by Dan Kibler (
[email protected])
* Extended to implement Acegi UserDetails interface
* by David Carter
[email protected] */
@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
}
<!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>