1

我正在使用struts spring 和 hibernate进行在线考试项目。在从registration.jsp 提交值时,我试图通过使用一个表的主键作为另一个表中的外键将这些详细信息插入两个不同的表中。但我只能在一个表中保存值(说主键表)。但我无法将详细信息保存在另一个表中。在控制台日志中,我可以看到以下异常,

2013-09-17 18:16:39 INFO  RegistrationAction:188 - Entering Into SaveUserDetails()
2013-09-17 18:16:39 INFO  class:25 - Entering Into saveUserRegistration()
2013-09-17 18:16:39 INFO  class:13 - Entering Into UserRegistrationDAO
Hibernate: insert into user_details (first_name, last_name, email, password, gender, dob, phone, experience) values (?, ?, ?, ?, ?, ?, ?, ?)
2013-09-17 18:16:39 INFO  RegistrationAction:214 - Entering Into setUserAddress()
2013-09-17 18:16:39 INFO  class:25 - Entering Into saveUserRegistration()
2013-09-17 18:16:39 INFO  class:13 - Entering Into UserRegistrationDAO
2013-09-17 18:16:39 ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class: onlineexam.beans.UserDetails, getter method of property: user_id
Sep 17, 2013 6:16:39 PM org.apache.catalina.core.ApplicationDispatcher invoke

注册动作.java

public String SaveUserDetails() {
            String forward = "success";

            try {
                logger.info("Entering Into SaveUserDetails()");//Log Information

                UserDetails s = new UserDetails();
                s.setFirst_name(getFirst_Name());
                s.setLast_name(getLast_Name());
                s.setEmail(getEmailid());
                s.setPassword(getPassWord());
                s.setGender(getGender());
                s.setDob(getDateofbirth());
                s.setPhone(getPhoneNo());
                s.setExperience(getUser_experience());
                userRegistrationService.saveUserRegistration(s);
                Set<UserAddress> address = new HashSet<UserAddress>(0);
                setUserAddress(address);


                logger.info("SuccessFull:Exiting from SaveUserDetails()");//Log Information
            } catch (Exception ex) {
                forward = "error";
            }

            return forward;

        }

        public void setUserAddress(Set<UserAddress> address) throws Exception {
            logger.info("Entering Into setUserAddress()");
               UserAddress ad = new UserAddress();
               ad.setAddr_line1(getAddr_line1());
               ad.setAddr_line2(getAddr_line2());
               ad.setAddr_line3(getAddr_line3());
               ad.setCity(getCity());
               ad.setZipcode(getZipcode());
               ad.setState(getState());
               ad.setCountry(getCountry());
               address.add(ad);
             userRegistrationService.saveUserRegistration(ad);
               logger.info("SuccessFull:Exiting from setUserAddress()");//Log Information
        }
    }

用户注册DAO.java

public class UserRegistrationDao extends HibernateDaoSupport {
    private static Logger logger = Logger.getLogger("UserRegistrationDao.class");
    public UserRegistrationDao() {}

    public UserDetails saveUserRegistration(UserDetails s) throws Exception {
        logger.info("Entering Into UserRegistrationDAO");
        return (UserDetails)getHibernateTemplate().merge(s);
    }   

}

用户注册服务.java

public class UserRegistrationService {

    private UserRegistrationDao userRegistrationDao;
    private static Logger logger=Logger.getLogger("UserRegistrationService.class");

    public void init() throws Exception {}

    public UserRegistrationDao getUserRegistrationDao() {
        logger.info("Entering into getUserRegistrationDao()");//Log information
        return userRegistrationDao;
    }

    public void setUserRegistrationDao(UserRegistrationDao userRegistrationDao) {
        this.userRegistrationDao = userRegistrationDao;
        logger.info("Entering Into setUserRegistrationDao()");//Log Information
    }

    public UserDetails saveUserRegistration (UserDetails user) throws Exception {
        logger.info("Entering Into saveUserRegistration()");//Log Information
        return userRegistrationDao.saveUserRegistration(user);
    }

}

用户详细信息.java

公共类用户详细信息 {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@OneToMany (mappedBy="user_details")

private int user_id; //primary key
private String first_name;
private String last_name;
private String email;
private String password;
private String gender;
private int dob;
private int phone;
private float experience;
//getters and setters created

用户地址.java

 public class UserAddress extends UserDetails {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int address_id; //primary key
        @ManyToOne(fetch=FetchType.EAGER, targetEntity=UserDetails.class)
        @JoinColumn(name="user_id")
        private int user_id; 
        private String addr_line1;
        private String addr_line2;
        private String addr_line3;
        private String city;
        private int zipcode;
        private String state;
        private String country;

    //getters and setters created
4

1 回答 1

0

你在做什么并不构成从toUserAddress的多对一关联。你只是添加到类。你必须做的是:UserAddressUserDetailsuser_idUserDetails

@ManyToOne(fetch=FetchType.EAGER, targetEntity=UserDetails.class)
@JoinColumn(name="user_id")
UserDetails userDetails;

public UserDetails getUserDetails() {
    return userDetails;
}

public void setUserDetails() {
    this.userDetails = userDetails;
}

我不明白为什么要UserAddress扩展UserDetails?我建议您阅读有关休眠关联的更多信息。

于 2013-09-17T13:27:04.130 回答