jpa单向一对多关联映射
2017-06-04 19:55
232 查看
如果在一的@OneToMany有@manyToOne则是双向一对多关联,如果在多的那面没有@manyToOne关联则是单向一对多关联
class和student是一对多的关系
表结构
student
class
Class
Student
Dao
测试
过程
第一个查询只能查出Student的数据,第二个可以把Class和Student的数据查出来(要设置fetch=FetchType.EAGER)
class和student是一对多的关系
表结构
student
class
Class
packageauth.model; importjava.util.ArrayList; importjava.util.List; importjavax.persistence.CascadeType; importjavax.persistence.Column; importjavax.persistence.Entity; importjavax.persistence.GeneratedValue; importjavax.persistence.GenerationType; importjavax.persistence.Id; importjavax.persistence.JoinColumn; importjavax.persistence.OneToMany; importjavax.persistence.Table; @Entity @Table(name="class") publicclassRoom{ privateintid; privateStringname; privateList<Student>students=newArrayList<Student>(); @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } @Column(name="name") publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } @OneToMany(cascade=CascadeType.ALL
,fetch=FetchType.EAGER
) @JoinColumn(name="room_id") publicList<Student>getStudents(){ returnstudents; } publicvoidsetStudents(List<Student>students){ this.students=students; } }
Student
packageauth.model; importjavax.persistence.Column; importjavax.persistence.Entity; importjavax.persistence.GeneratedValue; importjavax.persistence.GenerationType; importjavax.persistence.Id; importjavax.persistence.Table; @Entity @Table(name="student") publicclassStudent{ privateintid; publicintroom_id; privateStringname; @GeneratedValue(strategy=GenerationType.AUTO) @Id @Column(name="id") publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } @Column(name="room_id") publicintgetRoom_id(){ returnroom_id; } publicvoidsetRoom_id(introom_id){ this.room_id=room_id; } @Column(name="name") publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } }
Dao
packageauth.dao; importorg.springframework.data.jpa.repository.JpaRepository; importauth.model.MyClass; publicinterfaceClassDaoextendsJpaRepository<MyClass,Integer>{ }
测试
@Autowired privateClassDaodao; publicvoidadd(){ Roomroom=newRoom(); room.setName("5班"); Students=newStudent(); s.setName("小王"); room.getStudents().add(s); dao.save(room); Students1=newStudent(); s.setName("小李"); room.getStudents().add(s1); dao.save(room); }
publicvoidquery(){ Students=stuDao.findOne(2); System.out.println(JSONObject.toJSONString(s)); } publicvoidquery1(){ Rooms=dao.findOne(2); System.out.println(JSONObject.toJSONString(s)); }
过程
insert
into
class
(name)
values
(?)
2017-06-0423:56:04,251DEBUG[org.hibernate.id.IdentifierGeneratorHelper:94]-Nativelygeneratedidentity:3
2017-06-0423:56:04,272DEBUG[org.hibernate.engine.spi.ActionQueue:196]-Executingidentity-insertimmediately
2017-06-0423:56:04,273DEBUG[org.hibernate.SQL:109]-
insert
into
student
(room_id,name)
values
(?,?)
第一个查询只能查出Student的数据,第二个可以把Class和Student的数据查出来(要设置fetch=FetchType.EAGER)
相关文章推荐
- jpa单向多对多关联映射
- jpa单向多对一关联映射
- 【hibernate】之关于一对一单向,双向关联映射
- jpa 一对多单向关联 插入问题
- (六)Hibernate之单向一对一外键关联映射
- Hibernate的关联映射(1)-单向N-1关联
- Hibernate的关联映射(3)-单向1-N关联
- 一步步学习Hibernate框架(四):采用jpa实现一对多关联映射(二)
- Hibernate从入门到精通(五)一对一单向关联映射
- JPA关系映射系列五:many-to-many 关联表存在额外字段关系映射
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- Hibernate映射—— 一对一单向关联映射
- Hibernate关系映射级别注解(多对多单向外键关联、多对多双向外键关联)
- hibernate之关于一对多单向关联映射
- hibernate一对多关联映射—单向
- hibernate-关系映射(一对多单向关联)-9
- Hibernate一对多单向关联和双向关联映射方法及其优缺点
- Hibernate的关联映射---6.1.1: 单向n-1关联
- 【Hibernate】——单向关联映射