您好我正在尝试加入User
和Candidate
表。我想要来自firstname
表的列lastname
,email
,phone
,Candidate
,并加入password
表的User
列。我试过这样做,但得到了一些错误。我的代码如下:
CREATE TABLE `candidate` (`candidate_id` bigint(12) NOT NULL,`user_id` int(11) DEFAULT NULL,`firstname` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`lastname` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`dob` date DEFAULT NULL,`gender` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,`phone` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`address_id` int(11) DEFAULT NULL,`profile_title` varchar(120) COLLATE utf8_unicode_ci DEFAULT NULL,`profile_summary` varchar(10000) COLLATE utf8_unicode_ci DEFAULT NULL,`total_experience` int(11) DEFAULT NULL COMMENT 'year,month',`current_location` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`current_salary` double DEFAULT NULL,`expected_salary` double DEFAULT NULL,`status` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`percentage` int(45) DEFAULT NULL,`updated_on` timestamp NULL DEFAULT CURRENT_TIMESTAMP,`noticeperiod` int(11) DEFAULT NULL,`martialstatus` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`currentoffer` double DEFAULT NULL,`idproof` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`industry` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,`functionalarea` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,`functionalrole` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,`resign` bit(1) DEFAULT NULL,`preferlocation` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,`type` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,`notice_period_update` timestamp NULL DEFAULT NULL,`passport` bit(1) DEFAULT NULL,`gross_salary` double DEFAULT NULL,`profile_type` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,`rrm_user_id` int(11) DEFAULT NULL, PRIMARY KEY (`candidate_id`), KEY `Candidate_Address_idx` (`address_id`),KEY `Can_user` (`user_id`),KEY `CandidateStatus_idx` (`status`),KEY `CandidatePercent_idx` (`percentage`), KEY `user_id_idx` (`rrm_user_id`),CONSTRAINT `Can_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `Candidate_Address` FOREIGN KEY (`address_id`) REFERENCES `address`(`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `user_id` FOREIGN KEY (`rrm_user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我的用户表:
CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`password` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`first_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,`last_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,`type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`active` bit(1) DEFAULT NULL,`deleted` bit(1) DEFAULT NULL,`timezone` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`created_on` timestamp NULL DEFAULT NULL,`updated_on` timestamp NULL DEFAULT CURRENT_TIMESTAMP,`activationcode` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,`secure_key` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `username_UNIQUE` (`username`)) ENGINE=InnoDB AUTO_INCREMENT=1505 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
我的实体类:
@Table(name = "CANDIDATE")public class SignUp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String candidate_id;
@Column
private String firstname;
@Column
private String lastname;
@Column
private String email;
@Column
private String phone;
public String getCandidate_id() {
return candidate_id;
}
public void setCandidate_id(String candidate_id) {
this.candidate_id = candidate_id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "candidate_id")
private Password password;
public Password getPassword() {
return password;
}
public void setPassword(Password password) {
this.password = password;
}
对于用户表:
@Table(name = "USER")public class Password { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@Column
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private SignUp signUp;
public SignUp getSignUp() {
return signUp;
}
public void setSignUp(SignUp signUp) {
this.signUp = signUp;
}
获得的错误是:
Hibernate:
select
password0_.id as id1_1_0_,
password0_.password as password2_1_0_,
password0_.user_id as user_id4_1_0_,
signup1_.candidate_id as candidat1_0_1_,
signup1_.email as email2_0_1_,
signup1_.firstname as firstnam3_0_1_,
signup1_.lastname as lastname4_0_1_,
signup1_.phone as phone5_0_1_
from
User password0_
left outer join
CANDIDATE signup1_
on password0_.user_id=signup1_.candidate_id
where
password0_.id=?
2017年6月1日下午4:48:45 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL错误:1054,SQLState:42S22 Jun 01,2017 4:48:45 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions错误: 未知列' password0_.user_id'在'字段列表'
答案 0 :(得分:0)
您的映射很好,您只是错过了基于此映射的user_id
语句中的CREATE TABLE user
:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private SignUp signUp;
hibernate期望通过该列连接到CERTIFICATE
表。
另外不要忘记在DDL中添加外键语句。
答案 1 :(得分:0)
尝试使用Jboss逆向工程工具进行hibernate生成域并避免此类错误。这非常简单,您可以在5分钟内从数据库表创建域。