使用OneToOne注释创建额外列

huangapple go评论103阅读模式
英文:

Creating extra column with OneToOne annotation

问题

我有一个用户对象,我需要加载与用户对象一起的组对象。

我已经为此编写了以下代码。

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @Column(name="groupId")
  10. private int groupId;
  11. @OneToOne(cascade = CascadeType.ALL)
  12. @PrimaryKeyJoinColumn
  13. private GroupMaster groupMaster;
  14. //setters and getters
  15. }

GroupMaster

  1. @Entity
  2. @Table(name = "group_master")
  3. public class GroupMaster {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int groupId;
  7. @Column(name="groupName")
  8. private String groupName;
  9. //Setters and getters
  10. }

这里的 group_master 是主表,它有 groupIdgroupname

我会在 user_info 表中提供 groupId,基于该值,我需要获取 groupMaster 对象。但问题是 Hibernate 正在创建

  1. CREATE TABLE group_master
  2. (
  3. group_id integer NOT NULL,
  4. group_name character varying(255),
  5. user_user_id integer NOT NULL,
  6. CONSTRAINT group_master_pkey PRIMARY KEY (group_id ),
  7. CONSTRAINT fkjxrn8rglp972s1pqfh5tsb6cu FOREIGN KEY (user_user_id)
  8. REFERENCES user_info (user_id) MATCH SIMPLE
  9. ON UPDATE NO ACTION ON DELETE NO ACTION
  10. )
  11. WITH ( OIDS=FALSE );
  12. ALTER TABLE group_master OWNER TO XYZ;

我不知道为什么它创建了 user_user_id 列。我对这个 user_user_id 列没有任何用途。关系应该是与 groupId 相关的。我该如何修复这个问题。

英文:

I have an user object and I need to load group object along with the user object.

I have written for that the following code.

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @Column(name="groupId")
  10. private int groupId;
  11. @OneToOne(cascade = CascadeType.ALL)
  12. @PrimaryKeyJoinColumn
  13. private GroupMaster groupMaster;
  14. //setters and getters
  15. }

and GroupMaster:

  1. @Entity
  2. @Table(name = "group_master")
  3. public class GroupMaster {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int groupId;
  7. @Column(name="groupName")
  8. private String groupName;
  9. //Setters and getters
  10. }

Here the group_master is the master table and it has groupId and groupname.

I will give the groupId in user_info table based on that value I need to get the groupMaster object. But the problem here is hibernate is creating

  1. CREATE TABLE group_master
  2. (
  3. group_id integer NOT NULL,
  4. group_name character varying(255),
  5. user_user_id integer NOT NULL,
  6. CONSTRAINT group_master_pkey PRIMARY KEY (group_id ),
  7. CONSTRAINT fkjxrn8rglp972s1pqfh5tsb6cu FOREIGN KEY (user_user_id)
  8. REFERENCES user_info (user_id) MATCH SIMPLE
  9. ON UPDATE NO ACTION ON DELETE NO ACTION
  10. )
  11. WITH ( OIDS=FALSE );
  12. ALTER TABLE group_master OWNER TO XYZ;

I don't know why it creates user_user_id column. I don't have any use with this user_user_id column. The relation should be with groupId. How can I fix it.

答案1

得分: 1

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @OneToOne(cascade = CascadeType.ALL)
  10. private GroupMaster groupMaster;
  11. //setters and getters
  12. }
英文:

You don't need a separate groupId-column or the @PrimaryKeyJoinColumn Annotation
Change your UserDao to:

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @OneToOne(cascade = CascadeType.ALL)
  10. private GroupMaster groupMaster;
  11. //setters and getters
  12. }

答案2

得分: 0

你应该在双方都定义@OneToOne关系,因此你需要在GroupMaster中添加@OneToOne注解,以正确的方式进行操作,你应该像这样做:

在UserDao中:

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @OneToOne(mappedBy = "user")
  10. GroupMaster group;
  11. //setters and getters
  12. }

然后GroupMaster实体应该像这样:

  1. @Entity
  2. @Table(name = "group_master")
  3. public class GroupMaster {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int groupId;
  7. @Column(name="groupName")
  8. private String groupName;
  9. @OneToOne
  10. @PrimaryKeyJoinColumn
  11. UserDao user;
  12. //Setters and getters
  13. }
英文:

you should define the @OneToOne relationship in both sides so you need to add the @OneToOne annotation to the GroupMaster too, to make it in the properly way you should do it like this :

  1. @Entity
  2. @Table(name = "User_info")
  3. public class UserDao {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int userId;
  7. @Column(name="first_name")
  8. private String firstName;
  9. @OneToOne(mappedBy = "user")
  10. GroupMaster group;
  11. //setters and getters
  12. }

then the GroupMaster Entity should be like this :

  1. @Entity
  2. @Table(name = "group_master")
  3. public class GroupMaster {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private int groupId;
  7. @Column(name="groupName")
  8. private String groupName;
  9. @OneToOne
  10. @PrimaryKeyJoinColumn
  11. UserDao user;
  12. //Setters and getters
  13. }

huangapple
  • 本文由 发表于 2020年10月5日 17:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64205733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定