本文主要是介绍springboot中使用jpa时,实体中ManyToOne,OneToMany关系的注解设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实体1(Entity1):
实体1中有一个字段指向实体2中的id,于是添加Entity2 属性,将
@ManyToOne(targetEntity="Entity2.class", Fetch= FetchType.LAZY/FetchType.EAGER)
@joinColumn(name="当前表的对应关系字段", insertable=false, updatable=false, referencedColummName="id")
@JsonIgnoreProperties("当前类的实体(Entity1)")
Entity2 entity2;
实体2(Entity2):
@OneToMany(targetEntity= "Entity1.class", Fetch= FetchType.LAZY/FetchType.EAGER, mappedBy="entity1")
@JsonIgnore
Set<Entity1> entity1;
配置完成
测试查询Entity1 ,可通过Entity1中的Entity2 entity2;属性获取到对于的Entity2 信息
测试查询Entity2, 可通过Entity2中的Set<Entity1> entity1;获取到相关的Entity1信息
这篇关于springboot中使用jpa时,实体中ManyToOne,OneToMany关系的注解设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!