问题描述
我正在尝试使用Eclipse中的Hibernate Tools(注释)从MySQL数据库生成实体类.但是,我在生成一对一的关系代码时遇到了麻烦.我的MySQL表目前正在确保这种关系,但是Hibernate工具无法检测到它.一个用户与一个雇员完全关联"
这是我的表的代码.用户优先
CREATE TABLE`user`(`user_id` int(11)NOT NULL AUTO_INCREMENT,`username` varchar(20)NOT NULL,`password` varchar(45)NOT NULL,is_active` tinyint(1)非空默认值'1',`role_id` int(11)NOT NULL,emp_id int(11)非空主键(`user_id`),唯一键`employee_emp_id_UNIQUE`(`emp_id`),KEY`fk_user_role`(`role_id`),KEY`fk_user_employee1`(`emp_id`),约束`fk_user_role`外键(`role_id`)参考`role`(`role_id`)ON删除不采取行动,不执行更新不采取行动",约束`fk_user_employee1`外键(`emp_id`)参考`employee`(`emp_id`)删除时不执行任何操作删除时不执行任何操作)ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = latin1
第二名员工
CREATE TABLE`employee`(emp_id int(11)非空AUTO_INCREMENT,`name` varchar(45)NOT NULL,`address` varchar(45)默认为NULL,CNIC varchar(15)默认为空,`hourly_rate` int(11)NOT NULL,is_allowed tinyint(1)非空默认值'0',is_active` tinyint(1)非空默认值'1',`code` varchar(5)NOT NULL,主键(`emp_id`),唯一键`code_UNIQUE`(`code`))ENGINE = InnoDB AUTO_INCREMENT = 2 DEFAULT CHARSET = latin1
生成的代码是一对多...
对于用户类别
@Entity@Table(name ="user",catalog ="ieeepi_pharmacy",uniqueConstraints =@UniqueConstraint(columnNames ="emp_id"))公共类用户实现java.io.Serializable {private int userId;私人角色;私人雇员雇员;私有字符串用户名;私有字符串密码;私有布尔值isActive;@ID@GeneratedValue(策略=身份)@Column(name ="user_id",唯一= true,可为空= false)public int getUserId(){返回this.userId;}公共无效setUserId(int userId){this.userId = userId;}@ManyToOne(获取= FetchType.LAZY)@JoinColumn(name ="role_id",nullable = false)公共角色getRole(){返回this.role;}public void setRole(角色角色){this.role =角色;}@ManyToOne(获取= FetchType.LAZY)@JoinColumn(name ="emp_id",唯一= true,可为空= false)public Employee getEmployee(){返回此雇员;}public void setEmployee(Employee employee){this.employee =雇员;}@Column(name ="username",可为空= false,长度= 20)公共字符串getUsername(){返回this.username;}公共无效setUsername(String用户名){this.username =用户名;}@Column(name ="password",可为空= false,长度= 45)公共字符串getPassword(){返回this.password;}公共无效setPassword(字符串密码){this.password =密码;}@Column(name ="is_active",nullable = false)public boolean isIsActive(){返回this.isActive;}公共无效setIsActive(boolean isActive){this.isActive = isActive;}}
还有员工.
@Entity@Table(name ="employee",catalog ="ieeepi_pharmacy",uniqueConstraints =@UniqueConstraint(columnNames =代码"))公共类Employee实现java.io.Serializable {private int empId;私有字符串名称;私有字符串地址;私有String cnic;private int hourlyRate;私有布尔值isAllowed;私有布尔值isActive;私有字符串代码;私人Set< User>用户=新的HashSet< User>(0);私人套装< Attendance>出席人数=新的HashSet< Attendance>(0);@ID@GeneratedValue(策略=身份)@Column(name ="emp_id",唯一= true,可为空= false)public int getEmpId(){返回this.empId;}公共无效setEmpId(int empId){this.empId = empId;}@Column(name ="name",可为空= false,长度= 45)公共字符串getName(){返回this.name;}public void setName(String name){this.name =名称;}@Column(名称=地址",长度= 45)公共字符串getAddress(){返回this.address;}公共无效setAddress(字符串地址){this.address =地址;}@Column(名称="CNIC",长度= 15)公共字符串getCnic(){返回this.cnic;}公共无效setCnic(String cnic){this.cnic = cnic;}@Column(名称="hourly_rate",可为空=否)public int getHourlyRate(){返回this.hourlyRate;}public void setHourlyRate(int hourlyRate){this.hourlyRate = hourlyRate;}@Column(name ="is_allowed",可为空= false)public boolean isIsAllowed(){返回this.isAllowed;}公共无效setIsAllowed(boolean isAllowed){this.isAllowed = isAllowed;}@Column(name ="is_active",nullable = false)public boolean isIsActive(){返回this.isActive;}公共无效setIsActive(boolean isActive){this.isActive = isActive;}@Column(name ="code",唯一= true,可为空= false,长度= 5)公共字符串getCode(){返回this.code;}公共无效setCode(字符串代码){this.code =代码;}@OneToMany(提取= FetchType.LAZY,mappedBy =雇员")公共集< User>getUsers(){返回this.users;}public void setUsers(Set< User>用户){this.users =用户;}@OneToMany(提取= FetchType.LAZY,mappedBy =雇员")公共设置< Attendance>getAttendances(){返回this.attendances;}public void setAttendances(Set< Attendance>出勤率){this.attendances =出勤率;}}
您看到的结果是一对多...我还检查了检测一对一关联"选项,但无济于事...:(
解决此问题的关键是将父表的外键设置为子代的主键.
i am trying to generate Entity Classes from MySQL database using Hibernate Tools (Annotations) in Eclipse. However, I am having trouble in generating one-to-one relationship code. My MySQL table is currently ensuring this relationship yet Hibernate tools does not detect it...
"A User is Associated with Exactly One Employee"
Here is the code for my tables.. Users first
CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(45) NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`role_id` int(11) NOT NULL,
`emp_id` int(11) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `employee_emp_id_UNIQUE` (`emp_id`),
KEY `fk_user_role` (`role_id`),
KEY `fk_user_employee1` (`emp_id`),
CONSTRAINT `fk_user_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`role_id`) ON
DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_user_employee1` FOREIGN KEY (`emp_id`) REFERENCES `employee` (`emp_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
And Employee 2nd
CREATE TABLE `employee` (
`emp_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`address` varchar(45) DEFAULT NULL,
`CNIC` varchar(15) DEFAULT NULL,
`hourly_rate` int(11) NOT NULL,
`is_allowed` tinyint(1) NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`code` varchar(5) NOT NULL,
PRIMARY KEY (`emp_id`),
UNIQUE KEY `code_UNIQUE` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
The code generated is One to Many...
For User Class
@Entity
@Table(name = "user", catalog = "ieeepi_pharmacy", uniqueConstraints =
@UniqueConstraint(columnNames = "emp_id"))
public class User implements java.io.Serializable {
private int userId;
private Role role;
private Employee employee;
private String username;
private String password;
private boolean isActive;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id", nullable = false)
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "emp_id", unique = true, nullable = false)
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Column(name = "username", nullable = false, length = 20)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password", nullable = false, length = 45)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "is_active", nullable = false)
public boolean isIsActive() {
return this.isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
}
And for Employee..
@Entity
@Table(name = "employee", catalog = "ieeepi_pharmacy", uniqueConstraints =
@UniqueConstraint(columnNames = "code"))
public class Employee implements java.io.Serializable {
private int empId;
private String name;
private String address;
private String cnic;
private int hourlyRate;
private boolean isAllowed;
private boolean isActive;
private String code;
private Set<User> users = new HashSet<User>(0);
private Set<Attendance> attendances = new HashSet<Attendance>(0);
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "emp_id", unique = true, nullable = false)
public int getEmpId() {
return this.empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "address", length = 45)
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name = "CNIC", length = 15)
public String getCnic() {
return this.cnic;
}
public void setCnic(String cnic) {
this.cnic = cnic;
}
@Column(name = "hourly_rate", nullable = false)
public int getHourlyRate() {
return this.hourlyRate;
}
public void setHourlyRate(int hourlyRate) {
this.hourlyRate = hourlyRate;
}
@Column(name = "is_allowed", nullable = false)
public boolean isIsAllowed() {
return this.isAllowed;
}
public void setIsAllowed(boolean isAllowed) {
this.isAllowed = isAllowed;
}
@Column(name = "is_active", nullable = false)
public boolean isIsActive() {
return this.isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
@Column(name = "code", unique = true, nullable = false, length = 5)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
public Set<User> getUsers() {
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
public Set<Attendance> getAttendances() {
return this.attendances;
}
public void setAttendances(Set<Attendance> attendances) {
this.attendances = attendances;
}
}
The Result as you see is one to many... I Have also checked the "Detect One-to-one associations" option to no avail... :(
The key to solving the problem was to set the Foreign key of parent table as the primary key of the child.
这篇关于Hibernate Tools无法检测到一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!