hibernate annotation manytoone 单向
三范式:
第一范式:要有主键,列不可分
第二范式:联合范式,不能存在部分依赖
第三范式:不能存在传递依赖
解释:一个people对应多个dream
代码如下:
[dream] //many一方
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
//@Table(name="m_dream")
public class dream {
private int id ;
private String content;
private people people; //在many一方生成getter和setter方法
@ManyToOne //在many一方加注解 正好是manytoone
@JoinColumn(name="ss")
public people getPeople() {
return people;
}
public void setPeople(people people) {
this.people = people;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
[people]
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
//@Table(name="m_people")
public class people {
private int id ;
private String active;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
}
[test]
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.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();
}
}
利用反向工程得到的表: