Hibernate的关联映射问题

正在做多对一两张表关联,User和Userdetail两张表,User是“一”,Userdetail是“多”。
User的主键是uid,Userdetail主键是iduserdetail,外键是user_uid。每次我一启动tomcat就出现错误,说我的userdetailDAO不可写,或有无效的setter函数,困扰我多日。 :(
下面贴错误提示:
[quote]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userdetailBiz' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userdetailDAO' of bean class [com.tyyf.Biz.Impl.UserdetailBizImpl]: Bean property 'userdetailDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1114)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1672)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userdetailDAO' of bean class [com.tyyf.Biz.Impl.UserdetailBizImpl]: Bean property 'userdetailDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:801)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
... 30 more
[/quote]
以下是我的User.java代码:
[code="java"]
public class User implements java.io.Serializable {

// Fields

private Integer uid;
private String email;
private Integer emailcheck;
private String telephone;
private Integer telecheck;
private String accountcheck;
private String nickname;
private String password;
private String name;
private String sex;
private String height;
private String regdate;
private String lastlogindate;
private String beforelastdate;
private Integer locked;
private Integer level;
private String userip;
private Integer login;

// Constructors

/** default constructor */
public User() {
}

/** minimal constructor */
public User(Integer emailcheck, Integer telecheck, String accountcheck,
        String nickname, String password, String name, String sex,
        String height, String regdate, Integer locked, Integer level,
        Integer login) {
    this.emailcheck = emailcheck;
    this.telecheck = telecheck;
    this.accountcheck = accountcheck;
    this.nickname = nickname;
    this.password = password;
    this.name = name;
    this.sex = sex;
    this.height = height;
    this.regdate = regdate;
    this.locked = locked;
    this.level = level;
    this.login = login;
}

/** full constructor */
public User(String email, Integer emailcheck, String telephone,
        Integer telecheck, String accountcheck, String nickname,
        String password, String name, String sex, String height,
        String regdate, String lastlogindate, String beforelastdate,
        Integer locked, Integer level, String userip, Integer login) {
    this.email = email;
    this.emailcheck = emailcheck;
    this.telephone = telephone;
    this.telecheck = telecheck;
    this.accountcheck = accountcheck;
    this.nickname = nickname;
    this.password = password;
    this.name = name;
    this.sex = sex;
    this.height = height;
    this.regdate = regdate;
    this.lastlogindate = lastlogindate;
    this.beforelastdate = beforelastdate;
    this.locked = locked;
    this.level = level;
    this.userip = userip;
    this.login = login;
}

// Property accessors

public Integer getUid() {
    return this.uid;
}

public void setUid(Integer uid) {
    this.uid = uid;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

public Integer getEmailcheck() {
    return this.emailcheck;
}

public void setEmailcheck(Integer emailcheck) {
    this.emailcheck = emailcheck;
}

public String getTelephone() {
    return this.telephone;
}

public void setTelephone(String telephone) {
    this.telephone = telephone;
}

public Integer getTelecheck() {
    return this.telecheck;
}

public void setTelecheck(Integer telecheck) {
    this.telecheck = telecheck;
}

public String getAccountcheck() {
    return accountcheck;
}

public void setAccountcheck(String accountcheck) {
    this.accountcheck = accountcheck;
}

public String getNickname() {
    return this.nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public String getPassword() {
    return this.password;
}

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

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getSex() {
    return this.sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getHeight() {
    return this.height;
}

public void setHeight(String height) {
    this.height = height;
}

public String getRegdate() {
    return this.regdate;
}

public void setRegdate(String regdate) {
    this.regdate = regdate;
}

public String getLastlogindate() {
    return this.lastlogindate;
}

public void setLastlogindate(String lastdate) {
    this.lastlogindate = lastdate;
}

public String getBeforelastdate() {
    return beforelastdate;
}

public void setBeforelastdate(String beforelastdate) {
    this.beforelastdate = beforelastdate;
}

public Integer getLocked() {
    return this.locked;
}

public void setLocked(Integer locked) {
    this.locked = locked;
}

public Integer getLevel() {
    return this.level;
}

public void setLevel(Integer level) {
    this.level = level;
}

public String getUserip() {
    return this.userip;
}

public void setUserip(String userip) {
    this.userip = userip;
}

public Integer getLogin() {
    return this.login;
}

public void setLogin(Integer login) {
    this.login = login;
}

}
[/code]
Userdetail代码:
[code="java"]
public class Userdetail implements java.io.Serializable {

// Fields

private Integer iduserdetail;
private String education;
private String birthday;
private String workplace;
private String income;
private String maritalstatus;
private String childs;
private String housesituation;
private String identitycard;
private Integer identitycheck;
private String photo;
private String description;
private Integer user_uid;

// 使用User类声明user属性,并添加getter和setter方法,以体现实体类User和Userdetail的关联关系
private User user;

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

// Constructors

/** default constructor */
public Userdetail() {
}

/** minimal constructor */
public Userdetail(Integer iduserdetail, Integer identitycheck,
        Integer user_uid) {
    this.iduserdetail = iduserdetail;
    this.identitycheck = identitycheck;
}

/** full constructor */
public Userdetail(Integer iduserdetail, String education, String birthday,
        String workplace, String income, String maritalstatus,
        String childs, String housesituation, String identitycard,
        Integer identitycheck, String photo, String description,
        Integer user_uid) {
    this.iduserdetail = iduserdetail;
    this.education = education;
    this.birthday = birthday;
    this.workplace = workplace;
    this.income = income;
    this.maritalstatus = maritalstatus;
    this.childs = childs;
    this.housesituation = housesituation;
    this.identitycard = identitycard;
    this.identitycheck = identitycheck;
    this.photo = photo;
    this.description = description;
}

// Property accessors

public Integer getIduserdetail() {
    return this.iduserdetail;
}

public void setIduserdetail(Integer iduserdetail) {
    this.iduserdetail = iduserdetail;
}

public String getEducation() {
    return this.education;
}

public void setEducation(String education) {
    this.education = education;
}

public String getBirthday() {
    return this.birthday;
}

public void setBirthday(String birthday) {
    this.birthday = birthday;
}

public String getWorkplace() {
    return this.workplace;
}

public void setWorkplace(String workplace) {
    this.workplace = workplace;
}

public String getIncome() {
    return this.income;
}

public void setIncome(String income) {
    this.income = income;
}

public String getMaritalstatus() {
    return this.maritalstatus;
}

public void setMaritalstatus(String maritalstatus) {
    this.maritalstatus = maritalstatus;
}

public String getChilds() {
    return this.childs;
}

public void setChilds(String childs) {
    this.childs = childs;
}

public String getHousesituation() {
    return this.housesituation;
}

public void setHousesituation(String housesituation) {
    this.housesituation = housesituation;
}

public String getIdentitycard() {
    return this.identitycard;
}

public void setIdentitycard(String identitycard) {
    this.identitycard = identitycard;
}

public Integer getIdentitycheck() {
    return this.identitycheck;
}

public void setIdentitycheck(Integer identitycheck) {
    this.identitycheck = identitycheck;
}

public String getPhoto() {
    return this.photo;
}

public void setPhoto(String photo) {
    this.photo = photo;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public Integer getUser_uid() {
    return user_uid;
}

public void setUser_uid(Integer userUid) {
    user_uid = userUid;
}

}
[/code]
spring相关的代码:
[quote]

<bean id="userDAO" class="com.tyyf.DAO.impl.UserDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置创建UserdetailDAOImpl的实例 -->
<bean id="userdetailDAO" class="com.tyyf.DAO.impl.UserdetailDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

……

<!-- 依赖注入数据访问层组建 -->


<bean id="userdetailBiz" class="com.tyyf.Biz.Impl.UserdetailBizImpl">
    <property name="userdetailDAO" ref="userdetailDAO" />
</bean>

[/quote]

user.hbm.xml代码
[code="java"]



























































[/code]
Userdetail.hbm.xml代码
[code="java"]

catalog="mydb">








































[/code]
为了方便,我把userdetailDAOImpl也贴上来:
[code="java"]
public class UserdetailBizImpl implements UserdetailBiz {

UserdetailDAO userdetailDAO;

public void setUserdetail(UserdetailDAO userdetailDAO) {
    this.userdetailDAO = userdetailDAO;
}

@Override
public void deleteUser(Userdetail userdetail) {
    // TODO Auto-generated method stub
    userdetailDAO.deleteUserdetail(userdetail);
}

@Override
public Userdetail getUser(Userdetail userdetails) {
    // TODO Auto-generated method stub
    Userdetail userdetail = null;
    List list = userdetailDAO.searchUserdetail(userdetails);
    Iterator iter = list.iterator();
    if (iter.hasNext()) {
        userdetail = (Userdetail) iter.next();
    }
    return userdetail;
}

@Override
public void register(Userdetail userdetail) {
    // TODO Auto-generated method stub
    userdetailDAO.register(userdetail);
}

@Override
public void updateUsers(Userdetail userdetail) {
    // TODO Auto-generated method stub
    userdetailDAO.updateUserdetail(userdetail);
}

}
[/code]

2个回答

com.tyyf.Biz.Impl.UserdetailBizImpl中为userdetailDAO 添加set方法就好了

coolleon2010
coolleon2010 好吧,问题解决了,是我自己把set函数写掉了后面的“DAO”部分,正确写法应该是setUserdetailDAO(){……},果真是旁观者清,感激不尽
大约 6 年之前 回复
coolleon2010
coolleon2010 UserdetailBizImpl中贴图代码的第三行就是,已经设置好set方法了。
大约 6 年之前 回复

你映射错了,要用对象关联,Userdetail中的:
[code="java"]
private Integer user_uid;

[/code]
换成
[code="java"]
private User user;

[/code]

coolleon2010
coolleon2010 // 使用User类声明user属性,并添加getter和setter方法,以体现实体类User和Userdetail的关联关系 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } 这些已经在代码里了,另外user_uid在构造方法中已经把赋值代码删掉了,并且有getter和setter函数。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!