Hibernate Primary Key批注返回null值

时间:2015-06-17 16:42:38

标签: sql spring hibernate

我正在关注http://viralpatel.net/blogs/hibernate-one-to-one-mapping-tutorial-using-annotation/

用于学习主键注释。

我有两个班级

@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

private String firstName;
private String lastName;
private String email;
private String password;
private String profession;

@OneToOne(mappedBy="user" , cascade=CascadeType.ALL)
private Profile profile;

和我的个人资料类

@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int id; //profileId
private int userId;
private String specialization;
private String hospital;
private String clinicAddress;
private String clinicTime;
private String about;


@OneToOne
@PrimaryKeyJoinColumn
private User user;

两个表中都有条目,但我的个人资料值为null。任何人都可以告诉我我的代码中的问题是什么。我的桌子是

个人资料表是:

CREATE TABLE `profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) DEFAULT NULL,
`specialization` varchar(100) DEFAULT NULL,
`hospital` varchar(100) DEFAULT NULL,
`clinicAddress` varchar(100) DEFAULT NULL,
`clinicTime` varchar(12) DEFAULT NULL,
`about` varchar(300) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk` (`userId`),
KEY `pfk` (`userId`),
CONSTRAINT `pfk` FOREIGN KEY (`userId`) REFERENCES `user` (`id`) ON DELETE   NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1$$

和用户表是

CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
firstName varchar(45) DEFAULT NULL,
lastName varchar(45) DEFAULT NULL,
email varchar(45) DEFAULT NULL,
password varchar(45) DEFAULT NULL,
profession varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1$$

编辑:当我使用

@JoinColumn(name = "userId")
private User user;

并从Profile类中删除userId字段,它可以查找并获取配置文件对象。

现在,当我在Profile类中添加userId字段时,我得到了

Repeated column in mapping for entity: com.analyst.future.domain.Profile column: userId (should be mapped with insert="false" update="false")

UserProfileForm:

 UserProfileForm {

private int userId;
private String specialization;
private String hospital;
private String clinicAddress;
private String clinicTime;
private String about;

控制器:

public String registerNewUser(HttpServletRequest request, @ModelAttribute ("form") SignupForm form){
    User user = new User();
    try {
        BeanUtils.copyProperties(user, form);
    } catch (IllegalAccessException e) {    
        logger.error("ERROR : unable to copy values from form to user object");
        e.printStackTrace();
    } catch (InvocationTargetException e) {         
        e.printStackTrace();
    }

    Integer userId = userService.addUser(user);
    UserProfileForm profileForm = new UserProfileForm();
    profileForm.setUserId(userId);
    profileService.addProfile(profileForm);

    mailService.sendAccountCreatingNotification(user);

    return REDIRECT_CONFIRMATION_VIEW;
}

在profileService中:

public Profile addProfile(UserProfileForm form) {
    Profile profile = new Profile();
    try {
        BeanUtils.copyProperties(profile, form);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    return profileDAO.addProfile(profile);
}

并在ProfileDAOImpl

public Profile addProfile(Profile profile) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(profile);       
    return profile;
}

2 个答案:

答案 0 :(得分:1)

您的映射表明配置文件的ID也是一个连接列(即引用用户ID的外键)。这就是@PrimiryKeyJoinColumn的含义。

但是您的表定义显示您在配置文件中有一个userId列来引用配置文件的用户ID。所以你实际上应该使用@JoinColumn(name = "userId")

下定决心,选择你真正想做的事情。

答案 1 :(得分:1)

在Profile对象中,您有以下几行:

@OneToOne
@PrimaryKeyJoinColumn
private User user;

将这些更改为:

@JoinColumn(name = "userId")
private User user;

这里错误地使用了PrimaryKeyJoinColumn注释。您的数据库表不建议user_id是主键连接列。