英文:
Creating extra column with OneToOne annotation
问题
我有一个用户对象,我需要加载与用户对象一起的组对象。
我已经为此编写了以下代码。
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@Column(name="groupId")
private int groupId;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private GroupMaster groupMaster;
//setters and getters
}
和 GroupMaster
:
@Entity
@Table(name = "group_master")
public class GroupMaster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int groupId;
@Column(name="groupName")
private String groupName;
//Setters and getters
}
这里的 group_master
是主表,它有 groupId
和 groupname
。
我会在 user_info
表中提供 groupId
,基于该值,我需要获取 groupMaster
对象。但问题是 Hibernate 正在创建
CREATE TABLE group_master
(
group_id integer NOT NULL,
group_name character varying(255),
user_user_id integer NOT NULL,
CONSTRAINT group_master_pkey PRIMARY KEY (group_id ),
CONSTRAINT fkjxrn8rglp972s1pqfh5tsb6cu FOREIGN KEY (user_user_id)
REFERENCES user_info (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
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.
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@Column(name="groupId")
private int groupId;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private GroupMaster groupMaster;
//setters and getters
}
and GroupMaster
:
@Entity
@Table(name = "group_master")
public class GroupMaster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int groupId;
@Column(name="groupName")
private String groupName;
//Setters and getters
}
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
CREATE TABLE group_master
(
group_id integer NOT NULL,
group_name character varying(255),
user_user_id integer NOT NULL,
CONSTRAINT group_master_pkey PRIMARY KEY (group_id ),
CONSTRAINT fkjxrn8rglp972s1pqfh5tsb6cu FOREIGN KEY (user_user_id)
REFERENCES user_info (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH ( OIDS=FALSE );
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
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@OneToOne(cascade = CascadeType.ALL)
private GroupMaster groupMaster;
//setters and getters
}
英文:
You don't need a separate groupId-column or the @PrimaryKeyJoinColumn Annotation
Change your UserDao to:
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@OneToOne(cascade = CascadeType.ALL)
private GroupMaster groupMaster;
//setters and getters
}
答案2
得分: 0
你应该在双方都定义@OneToOne关系,因此你需要在GroupMaster中添加@OneToOne注解,以正确的方式进行操作,你应该像这样做:
在UserDao中:
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@OneToOne(mappedBy = "user")
GroupMaster group;
//setters and getters
}
然后GroupMaster实体应该像这样:
@Entity
@Table(name = "group_master")
public class GroupMaster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int groupId;
@Column(name="groupName")
private String groupName;
@OneToOne
@PrimaryKeyJoinColumn
UserDao user;
//Setters and getters
}
英文:
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 :
@Entity
@Table(name = "User_info")
public class UserDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userId;
@Column(name="first_name")
private String firstName;
@OneToOne(mappedBy = "user")
GroupMaster group;
//setters and getters
}
then the GroupMaster Entity should be like this :
@Entity
@Table(name = "group_master")
public class GroupMaster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int groupId;
@Column(name="groupName")
private String groupName;
@OneToOne
@PrimaryKeyJoinColumn
UserDao user;
//Setters and getters
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论