我们正在建设一个由多个项目组成的复杂项目。在这些项目中,其中两个通常用于定义基本实体(如接口)。
通常情况下,如果这些项目都是一个项目,那么下面的代码就会运行而没有任何错误。
但是将这些代码放在单独的项目中,从超类派生的实体(来自BaseEntity)会导致此错误:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.unalcable.department.security.model.ApprovalHistory
所以我搜索了原因,并且我发现在Hibernate的配置中定义BaseEntity就像其他类似的具体类会修复它。但它没有......
以下是代码:
Hibernate的配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.unalcable.general.common.model.BaseEntity</value>
<value>com.unalcable.department.security.model.ApprovalHistory</value>
<value>com.unalcable.department.security.model.CarVisitorRequest</value>
<value>com.unalcable.department.security.model.HumanVisitorRequest</value>
<value>com.unalcable.department.security.model.Privilige</value>
<value>com.unalcable.department.security.model.Visitor</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.showSql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.action}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.globally_quoted_identifiers">true</prop>
</props>
</property>
</bean>
BaseEntity.java(在项目1中)
@MappedSuperclass
@Access(AccessType.PROPERTY)
public class BaseEntity implements Serializable
{
public static final long serialVersionUID = 1L;
public Long id;
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
}
public boolean equals(Object obj)
{
boolean result = false;
if (obj instanceof BaseEntity) {
BaseEntity other = (BaseEntity)obj;
if (other.getId().equals(this.getId())) result = true;
}
return result;
}
public String toDebugString()
{
return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
ApprovalHistory.java(项目2)
@Entity
public class ApprovalHistory extends BaseEntity {
public long date;
public User user;
@Enumerated(EnumType.STRING)
public ApprovalState approvalState;
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public ApprovalState getApprovalState() {
return approvalState;
}
public void setApprovalState(ApprovalState approvalState) {
this.approvalState = approvalState;
}
}
**所有项目都在构建路径中引用必要的项目。 Tomcat也在类路径**
中有这些项目答案 0 :(得分:0)
您获得的错误可能是由混合访问类型引起的。 BaseEntity声明@Access(AccessType.PROPERTY)
,而ApprovalHistory有一个带注释的字段,因此Hibernate假定它使用FIELD访问类型。我不确定这一点,但也可能尝试将FIELD访问权限应用于您的基本实体,因此无法找到标识符。
尝试将@Enumerated
注释移至getApprovalState()
方法以查看是否属实。