谭明智的空间

我们一直在努力....

JPA主键注解的位置为什么加载getter上报错?

标签: JPA EJB Hibernate 注解
在使用ejb开发的过程中,莫名奇妙的出现如下错误。
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property pk in class org.oecp.core.domain.permission.UserEntity
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:240)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:233)
at org.hibernate.mapping.Property.getSetter(Property.java:299)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:272)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:149)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:76)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:325)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:457)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:261)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
... 50 more
UserEntity:
@Entity
@Table(name = "t_c_user")
public class UserEntity extends BaseEntityBean {

private Integer userId;
private String userName;
private String password;

@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.TABLE)
public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

@Column(name = "user_name")
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Column
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
BaseEntityBean:
@MappedSuperclass
public abstract class BaseEntityBean implements Serializable {

private static final long serialVersionUID = 1962905939086138888L;

/** 更新时间 **/
@Temporal(value=TemporalType.TIMESTAMP)
private Date updateTime;

/** 是否删除 **/
private boolean deleted;



@Embedded
public EditState editstate ;


@Column(name = "update_time")
public Date getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}

@Column(name = "deleted")
public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}


private BaseEntityBeanUtility givemeBeanUtility(){
return new BaseEntityBeanUtility(this);
}

@Override
public String toString() {
return givemeBeanUtility().beanToString();
}

@Override
public boolean equals(Object obj) {
return givemeBeanUtility().equalsBean(obj);
}

@Override
public int hashCode() {
return givemeBeanUtility().hashCodeBean();
}

@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = null;
try {
obj = givemeBeanUtility().cloneBean();
} catch (Exception e) {
throw new CloneNotSupportedException(StackTraceUtil.getStackTrace(e));
}

return obj;
}

public Object getAttributeValue(String attributeName){
return givemeBeanUtility().getAttributeVlaue(attributeName);
}

/**
* 获取主键值
* TODO 此方法应为抽象方法,由子类覆盖。为调试代码暂时不做抽象。以后修改为抽象方法,各子类应自己实现。
* @author slx
* @date 2009-6-12 上午09:15:11
* @modifyNote
* @return
*/
@Enumerated
public String myPk(){
return "返回主键值";
}
}
才开始在BaseEntityBean中有个getPk()方法,以为是这里的错误,后来加上@Enumerated注解还是不行,再后来将getPk()改为myPk(),还是不行。
最后将UserEntity中的@Id注解,由getUserId()转移到属性userId上面,结果服务器就能正常运行了。实在搞不明白这是为什么?
注解是可以加在getter方法上的啊,哪位高人给小弟指点一二。
 
问题已经解决,是@Enumerated 和@Embedded 的使用错误,改成  @Transient就可以了
再次感谢网上朋友的帮助,给我提供了一篇非常好的文章,我已经发布在知识库中了。
 
 

    分享: 收藏到CSDN 收藏到javaeye 收藏到博客园 收藏&分享
  • 浏览 (434)
  • 评论 (0)
  • 发表于 2009-06-16 08:52
  • Java
    评分: 请先登录再投票,同一篇博客一月只能投票一次!
    无人投票

相关博客:


评论


发表评论

关注此文的人们还关注