Hibernate One-To-One DAO

时间:2013-10-01 12:40:44

标签: spring hibernate model-view-controller dao one-to-one

我有两个实体类:

  • 用户:用户凭据。
  • UserInfo :用户的anagraphic信息。

和一个 DAO

  • UserDAO :Dao for class User。

它们通过主键与OneToOne关系链接为外键。所以User.id是User和UserInfo的主键。

数据提取运行良好但问题是:当我使用userDAO保存用户时,UserInfo不知道必须保存的主键。

如何告诉UserInfo等待用户保存以了解其共享主键?

我有这个例外

    Request processing failed; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.marciani.sample.entity.user.model.UserInfo.user]

提前谢谢!

这是代码:

用户

    @Entity
    @Table(name = "users")
    public class User implements Serializable {

        private static final long serialVersionUID = -7351729135012380019L;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "user_id", nullable = false, unique = true)
        private Integer id;

        @Column(name = "username", nullable = false, unique = true, length = 25)
        private String username;

        @Column(name = "password", nullable = false, unique = false, length = 25)
        private String password;

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "role_id", nullable = false)
        @ForeignKey(name = "fk_user_role")
        private Role role;  

        @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
        private UserInfo userInfo;

        public User(String username, String password, Role role, UserInfo userInfo) {
            this.username = username;
            this.password = password;
            this.role = role;
            this.userInfo = userInfo;
        }

        public User() {}

            //Getters/Setters ...
    }

的UserInfo

    @Entity
    @Table(name = "users_info")
    public class UserInfo implements Serializable {

        private static final long serialVersionUID = 3977944889257599040L;

        @Id     
        @GeneratedValue(generator="gen")
        @GenericGenerator(name="gen", strategy="foreign", parameters={@Parameter(name="property", value="user")})
        private Integer userId;

        @Column(name = "firstname", nullable = false, unique = false, length = 25)
        private String firstname;

        @Column(name = "lastname", nullable = false, unique = false, length = 25)
        private String lastname;

        @Column(name = "email", nullable = false, unique = false, length = 50)
        private String email;

        @OneToOne
        @MapsId
        private User user;

        public UserInfo(String firstname, String lastname, String email) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.email = email;
        }

        public UserInfo() {}

            //Getters/Setters

    }

UserService

    @Service("userService")
    public class UserServiceImpl implements UserService {

        @Autowired
        private UserDAO userDAO;

        @Override
        @Transactional(readOnly = false)
        public void saveUser(User user) {
            userDAO.saveUser(user); 
        }

        @Override
        @Transactional(readOnly = false)
        public void deleteUser(String username) {
            User user = findByUsername(username);
            userDAO.deleteUser(user);       
        }

        @Override
        @Transactional(readOnly = true)
        public User findByUsername(String username) {
            Criterion criterion = Restrictions.eq("username", username);
            List<User> list = userDAO.find(criterion);
            return list.get(0);
        }

        @Override
        @Transactional(readOnly = true)
        public User getUser(String username, String password) {
            Criterion criterion = Restrictions.conjunction()
            .add(Restrictions.eq("username", username))
            .add(Restrictions.eq("password", password));
            List<User> list = userDAO.find(criterion);
            if (list.size() > 0) {
                return list.get(0);
            } else {
                return null;
            }       
        }

        @Override
        @Transactional(readOnly = true)
        public List<User> findAll() {
            return userDAO.findAll();
        }

    }

UserDAO的

    @Repository
    public class UserDAOImpl implements UserDAO {

        @Autowired
        public SessionFactory sessionFactory;

        public Session getCurrentSession() {
            return sessionFactory.getCurrentSession();
        }   

        @Override
        public void saveUser(User user) {
            getCurrentSession().saveOrUpdate(user);;        
        }

        @Override
        public void deleteUser(User user) {
            getCurrentSession().delete(user);
        }

        @Override
        public List<User> find(Criterion criterion) {
            Criteria criteria = getCurrentSession().createCriteria(User.class);
            criteria.add(criterion);
            return criteria.list();
        }

        @Override
        public List<User> findAll() {
            Criteria criteria = getCurrentSession().createCriteria(User.class);
            return criteria.list();
        }

    }

1 个答案:

答案 0 :(得分:0)

您在哪里发起此用户ID( private Integer id;)

你也没有提到@column UserInfo类的userId