hibernate annotation 组件映射
让Customer从属于Users这一张表,即让Customer属于Users表的一部分,用Embedded注解,意思为“嵌入的”,注意只有一个id。
代码如下:
[Customer]
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Customer {
private String customername;
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
[Users]
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Users {
private int id;
private String level;
private String title;
private Customer customer;
@Embedded
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getLevel() {
return level;
}
public String getTitle() {
return title;
}
public void setId(int id) {
this.id = id;
}
public void setLevel(String level) {
this.level = level;
}
public void setTitle(String title) {
this.title = title;
}
}
[test]
public class testUsers {
@Test
public void testSchemaExport() {
// SessionFactory sessionFactory = new AnnotationConfiguration()
// .configure().buildSessionFactory();
SchemaExport export = new SchemaExport(
new AnnotationConfiguration().configure());
export.create(true, false);
// Session session = sessionFactory.getCurrentSession();
// session.beginTransaction();
// session.getTransaction().commit();
}
}