2011-07-17 16 views
2

EclipseでHibernate Tools(注釈)を使用してMySQLデータベースからエンティティクラスを生成しようとしています。しかし、私は一対一の関係コードを生成する際に問題があります。私のMySQLのテーブルには、現在ここでHibernateツールが1対1の関係を検出できない

「ユーザーが正確に一つの従業員に関連付けられている」...それを検出していないツールを休止まだ

をこの関係を確保している私のテーブル用のコード..ですユーザー

最初
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 

と従業員の第二

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 

生成されたコードは、多くの一つである...

ユーザークラスの

@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; 
} 

} 

と従業員のために...

@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; 
} 

} 

結果、あなたが見るように、「一対一の関連を検出」私もチェックしている...多くの1つですオプションを使用しない... :(

答えて

1

問題を解決するための鍵は、親テーブルの外部キーを子の主キーとして設定することでした。

+0

これを説明できます。私は同じ問題に直面している – ArslanAnjum