org.hibernate.AnnotationException:使用@OneToMany 或@ManyToMany 定位未映射的 class:core.user.User.userRole[core.user.UserRole]
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: core.user.User.userRole[core.user.UserRole]
这些实体 类 出现此错误。我查看了 this question,但我没有 persistence.xml
,所以我不确定如何解决这个问题。如果有帮助,我正在使用 Dropwizard 构建这些实体。
问题似乎在于我如何将 User
映射到 UserRole
。两者 类 在同一个包中。
User.java
@Entity
@Table(name = "USER")
public class User implements TimestampedItem {
//region Fields
protected String email;
protected String password;
protected String displayName;
protected Long ID;
protected Set<UserRole> userRole;
protected Boolean isActive = true;
protected Boolean isEmailConfirmed = false;
protected DateTime added = new DateTime();
protected DateTime modified = new DateTime();
//endregion
//region Constructors
public User() {
this("", "", "");
}
public User(String email, String password, String displayName) {
this(email, password, displayName, true);
}
public User(String email, String password, String displayName, Boolean isActive) {
this(email, password, displayName, new HashSet<>(), isActive, false);
}
public User(String email, String password, String displayName, Set<UserRole> userRole, Boolean isActive, Boolean isEmailConfirmed) {
this(email, password, displayName, userRole, isActive, isEmailConfirmed, new DateTime(), new DateTime());
}
public User(String email, String password, String displayName, Set<UserRole> userRole, Boolean isActive, Boolean isEmailConfirmed, DateTime added, DateTime modified) {
this.email = email;
this.password = password;
this.displayName = displayName;
this.userRole = userRole;
this.isActive = isActive;
this.isEmailConfirmed = isEmailConfirmed;
this.added = added;
this.modified = modified;
}
//endregion
//region Getters and Setters
/**
* Returns this Users email address
*
* @return this Users email address
*/
@Email
@Column(name = "EMAIL", unique = true, nullable = false, length = 255)
public String getEmail() {
return this.email;
}
/**
* Sets this Users email address
*
* @param email The email address to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Returns this User's password
*
* @return this User's password
*/
@Column(name = "PASSWORD", nullable = false, length = Passwords.DESIRED_KEY_LENGTH)
public String getPassword() {
return this.password;
}
/**
* Sets this User's password
*
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns this User's unique User ID
*
* @return this User's unique User ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID", unique = true, nullable = false)
public Long getID() {
return this.ID;
}
/**
* Sets this User's unique User ID
*
* @param id The unique ID to set to this user
*/
public void setID(Long id) {
this.ID = id;
}
/**
* Whether this User's profile is isActive
*
* @return true if this User's profile is isActive, false otherwise
*/
@Column(name = "IS_ACTIVE", nullable = false)
public Boolean isActive() {
return this.isActive;
}
/**
* Sets whether this User's profile is isActive
*
* @param active true if this User's profile is isActive, false otherwise
*/
public void setActive(Boolean active) {
this.isActive = active;
}
/**
* Whether this User's email address has been confirmed.
* To confirm an email, they must click the activation link that is sent to their email address
*
* @return true if this User's email has been confirmed, false otherwise
*/
@Column(name = "IS_EMAIL_CONFIRMED", nullable = false)
public Boolean isEmailConfirmed() {
return this.isEmailConfirmed;
}
/**
* Sets whether this User's email address has been confirmed.
* To confirm an email, they must click the activation link that is sent to their email address
*
* @param isEmailConfirmed true if this User's email has been confirmed, false otherwise
*/
public void setEmailConfirmed(Boolean isEmailConfirmed) {
this.isEmailConfirmed = isEmailConfirmed;
}
/**
* Returns this User's display name
*
* @return this User's display name
*/
@Column(name = "DISPLAY_NAME", nullable = false)
public String getDisplayName() {
return this.displayName;
}
/**
* Sets this User's display name
*
* @param displayName The display name to associate with this User
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserRole> getUserRole() {
return this.userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
@Override
@Column(name = "ADDED", nullable = false)
public DateTime getAdded() {
return this.added;
}
@Override
public void setAdded(DateTime added) {
this.added = added;
}
@Override
@Column(name = "MODIFIED", nullable = false)
public DateTime getModified() {
return this.modified;
}
@Override
public void setModified(DateTime modified) {
this.modified = modified;
}
//endregion
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
// These probably aren't needed, since a user is considered unique by ID alone
// if (!displayName.equals(user.displayName)) return false;
// if (!email.equals(user.email)) return false;
// if (!password.equals(user.password)) return false;
return getID().equals(user.getID());
}
@Override
public int hashCode() {
int result = email.hashCode();
result = 31 * result + password.hashCode();
result = 31 * result + displayName.hashCode();
return result;
}
}
UserRole.java
@Entity
@Table(
name = "user_roles"
, uniqueConstraints = @UniqueConstraint( columnNames = { "role", "USER_ID" } )
)
public class UserRole{
private Integer userRoleId;
private User user;
private String role;
public UserRole() {
}
public UserRole(User user, String role) {
this.user = user;
this.role = role;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_role_id", unique = true, nullable = false)
public Integer getUserRoleId() {
return this.userRoleId;
}
public void setUserRoleId(Integer userRoleId) {
this.userRoleId = userRoleId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@Column(name = "role", nullable = false, length = 45)
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
}
}
您使用 spring 框架吗?如果你使用这个,你可以将这段代码添加到 appliactionContext.xml
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="dbke" />
<property name="PersistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml" />
</bean>
您需要创建 jpa-persistence.xml
firstly.For 示例:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="dbke" transaction-type="RESOURCE_LOCAL">
<class>core.user.Userrole</class>
<class>core.user.User</class>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
您是否正确设置了 HibernateBundle? Dropwizard Hibernate offers two bundles to set up the persistence unit with hibernate, one being the default HibernateBundle 需要在构造函数中传递的所有实体 类 - 是否都传递了 类?
或者,您可以使用 ScanningHibernateBundle 并将您的实体的包名称作为字符串传入,这样您就不会在将来遗漏任何内容。这在您的应用程序中看起来像这样:
private final HibernateBundle<MyConfiguration> hibernate =
new ScanningHibernateBundle<MyConfiguration>("com.acme.entity") {
@Override
public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
这些实体 类 出现此错误。我查看了 this question,但我没有 persistence.xml
,所以我不确定如何解决这个问题。如果有帮助,我正在使用 Dropwizard 构建这些实体。
问题似乎在于我如何将 User
映射到 UserRole
。两者 类 在同一个包中。
User.java
@Entity
@Table(name = "USER")
public class User implements TimestampedItem {
//region Fields
protected String email;
protected String password;
protected String displayName;
protected Long ID;
protected Set<UserRole> userRole;
protected Boolean isActive = true;
protected Boolean isEmailConfirmed = false;
protected DateTime added = new DateTime();
protected DateTime modified = new DateTime();
//endregion
//region Constructors
public User() {
this("", "", "");
}
public User(String email, String password, String displayName) {
this(email, password, displayName, true);
}
public User(String email, String password, String displayName, Boolean isActive) {
this(email, password, displayName, new HashSet<>(), isActive, false);
}
public User(String email, String password, String displayName, Set<UserRole> userRole, Boolean isActive, Boolean isEmailConfirmed) {
this(email, password, displayName, userRole, isActive, isEmailConfirmed, new DateTime(), new DateTime());
}
public User(String email, String password, String displayName, Set<UserRole> userRole, Boolean isActive, Boolean isEmailConfirmed, DateTime added, DateTime modified) {
this.email = email;
this.password = password;
this.displayName = displayName;
this.userRole = userRole;
this.isActive = isActive;
this.isEmailConfirmed = isEmailConfirmed;
this.added = added;
this.modified = modified;
}
//endregion
//region Getters and Setters
/**
* Returns this Users email address
*
* @return this Users email address
*/
@Email
@Column(name = "EMAIL", unique = true, nullable = false, length = 255)
public String getEmail() {
return this.email;
}
/**
* Sets this Users email address
*
* @param email The email address to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Returns this User's password
*
* @return this User's password
*/
@Column(name = "PASSWORD", nullable = false, length = Passwords.DESIRED_KEY_LENGTH)
public String getPassword() {
return this.password;
}
/**
* Sets this User's password
*
* @param password The password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns this User's unique User ID
*
* @return this User's unique User ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID", unique = true, nullable = false)
public Long getID() {
return this.ID;
}
/**
* Sets this User's unique User ID
*
* @param id The unique ID to set to this user
*/
public void setID(Long id) {
this.ID = id;
}
/**
* Whether this User's profile is isActive
*
* @return true if this User's profile is isActive, false otherwise
*/
@Column(name = "IS_ACTIVE", nullable = false)
public Boolean isActive() {
return this.isActive;
}
/**
* Sets whether this User's profile is isActive
*
* @param active true if this User's profile is isActive, false otherwise
*/
public void setActive(Boolean active) {
this.isActive = active;
}
/**
* Whether this User's email address has been confirmed.
* To confirm an email, they must click the activation link that is sent to their email address
*
* @return true if this User's email has been confirmed, false otherwise
*/
@Column(name = "IS_EMAIL_CONFIRMED", nullable = false)
public Boolean isEmailConfirmed() {
return this.isEmailConfirmed;
}
/**
* Sets whether this User's email address has been confirmed.
* To confirm an email, they must click the activation link that is sent to their email address
*
* @param isEmailConfirmed true if this User's email has been confirmed, false otherwise
*/
public void setEmailConfirmed(Boolean isEmailConfirmed) {
this.isEmailConfirmed = isEmailConfirmed;
}
/**
* Returns this User's display name
*
* @return this User's display name
*/
@Column(name = "DISPLAY_NAME", nullable = false)
public String getDisplayName() {
return this.displayName;
}
/**
* Sets this User's display name
*
* @param displayName The display name to associate with this User
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserRole> getUserRole() {
return this.userRole;
}
public void setUserRole(Set<UserRole> userRole) {
this.userRole = userRole;
}
@Override
@Column(name = "ADDED", nullable = false)
public DateTime getAdded() {
return this.added;
}
@Override
public void setAdded(DateTime added) {
this.added = added;
}
@Override
@Column(name = "MODIFIED", nullable = false)
public DateTime getModified() {
return this.modified;
}
@Override
public void setModified(DateTime modified) {
this.modified = modified;
}
//endregion
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
// These probably aren't needed, since a user is considered unique by ID alone
// if (!displayName.equals(user.displayName)) return false;
// if (!email.equals(user.email)) return false;
// if (!password.equals(user.password)) return false;
return getID().equals(user.getID());
}
@Override
public int hashCode() {
int result = email.hashCode();
result = 31 * result + password.hashCode();
result = 31 * result + displayName.hashCode();
return result;
}
}
UserRole.java
@Entity
@Table(
name = "user_roles"
, uniqueConstraints = @UniqueConstraint( columnNames = { "role", "USER_ID" } )
)
public class UserRole{
private Integer userRoleId;
private User user;
private String role;
public UserRole() {
}
public UserRole(User user, String role) {
this.user = user;
this.role = role;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_role_id", unique = true, nullable = false)
public Integer getUserRoleId() {
return this.userRoleId;
}
public void setUserRoleId(Integer userRoleId) {
this.userRoleId = userRoleId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", nullable = false)
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
@Column(name = "role", nullable = false, length = 45)
public String getRole() {
return this.role;
}
public void setRole(String role) {
this.role = role;
}
}
您使用 spring 框架吗?如果你使用这个,你可以将这段代码添加到 appliactionContext.xml
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="dbke" />
<property name="PersistenceXmlLocation" value="classpath:META-INF/jpa-persistence.xml" />
</bean>
您需要创建 jpa-persistence.xml
firstly.For 示例:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="dbke" transaction-type="RESOURCE_LOCAL">
<class>core.user.Userrole</class>
<class>core.user.User</class>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
您是否正确设置了 HibernateBundle? Dropwizard Hibernate offers two bundles to set up the persistence unit with hibernate, one being the default HibernateBundle 需要在构造函数中传递的所有实体 类 - 是否都传递了 类?
或者,您可以使用 ScanningHibernateBundle 并将您的实体的包名称作为字符串传入,这样您就不会在将来遗漏任何内容。这在您的应用程序中看起来像这样:
private final HibernateBundle<MyConfiguration> hibernate =
new ScanningHibernateBundle<MyConfiguration>("com.acme.entity") {
@Override
public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};