Hibernate Tools fails to detect One-to-One Relationship

青春壹個敷衍的年華 提交于 2021-01-27 19:39:25

问题


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... :(


回答1:


The key to solving the problem was to set the Foreign key of parent table as the primary key of the child.




回答2:


A lot of time has passed since the question but i want to ask a question. I have kinda similar DB relations but the difference is i have employees, field employees and 3rd party employees which all three of them areusers for my structure. So i'm planning to create a userId field with a foreign key to PK of users table instead the reverse(emp_id in the users table) implemented in the question above which is a slightly diferent case. Which one is a better approach for this kind of a structure.

And also i couldn' figure out the solution to manage a one to one relation between users and the employees, field employees and 3rd party employees.



来源:https://stackoverflow.com/questions/6723918/hibernate-tools-fails-to-detect-one-to-one-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!