我想用共享主键创建双向一对一关系。
正如这里所说,JPA Hibernate 一对一关系我有:
@Entity
public class UserProfileInformation {
@Id
@GeneratedValue(generator = "customForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
name = "customForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "userEntity")
)
long id;
private long itemsPerPage;
@OneToOne(mappedBy="userProfileInformation")
private UserEntity userEntity;
...}
@Entity
@Table(name = "UserTable")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String publicName;
private String password;
private String emailAddress;
private String name;
private boolean active;
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private UserProfileInformation userProfileInformation;
...}
现在,当我尝试将我的用户保存在数据库中时,我得到org.hibernate.id.IdentifierGenerationException: null id generated for:class pl.meble.taboret.model.UserProfileInformation
. 是因为,当 userProfileInformation 持久化到数据库 userEntity 时没有生成 id 吗?
另外,在我的示例中,如何创建与共享主键的双向关系?
编辑:请求的代码,这是测试持久用户实体操作的简单控制器。
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
UserDao userDao;
@RequestMapping(method= RequestMethod.GET)
public String t(Model model){
UserEntity entity=new UserEntity();
entity.setActive(false);
entity.setEmailAddress("a");
entity.setName("name");
entity.setPassword("qqq");
entity.setPublicName("p");
UserProfileInformation p = new UserProfileInformation(entity);
entity.setUserProfileInformation(p);
userDao.addUser(entity);
return "login";
}
}