spring boot jpa 一对多关系子表查询多次
我有2个表,一对多关系:
t_damage_lodition 主表
t_damage_cabin 子表
子表里有个外键是主表里面得主键,用得mysql数据库,但是数据库中未建立主外键关系。
代码如下
表t_damage_lodition对应的实体:
t_damage_cabin实体:
Dao:
Service:
测试结果:

从日志可以看出先查出主表,我主表里面有3条记录,然后循环所有的主表记录去查子表。这样效率好低,为啥没直接生成join语句,直接执行一次查询?还是说我整错了?
t_damage_lodition 主表
t_damage_cabin 子表
子表里有个外键是主表里面得主键,用得mysql数据库,但是数据库中未建立主外键关系。
代码如下
表t_damage_lodition对应的实体:
@Entity
@Data
@Table(name="t_damage_lodition")
public class DamageLodition extends AbstractEntity{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer damageid;
Integer UserId;
String loaditionName;
Date createDate;
Date modifyDate;
String locakFlag;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="damageid",referencedColumnName="damageid")
List<DamageCabin> damageCabinList;
}
t_damage_cabin实体:
@Entity
@Data
@Table(name="t_damage_cabin")
public class DamageCabinextends AbstractEntity{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;
Integer damageid;
String cabinName;
Date strTankName;
}
Dao:
public interface DamageLoditionDao extends JpaRepository<DamageLodition ,Integer>{}
Service:
public class DamageLoditionService {
@Autowired
DamageLoditionDao damageLoditionDao;
public List<DamageLodition> SelectAll()
{
return damageLoditionDao.findAll();
}
}
测试结果:

从日志可以看出先查出主表,我主表里面有3条记录,然后循环所有的主表记录去查子表。这样效率好低,为啥没直接生成join语句,直接执行一次查询?还是说我整错了?
...全文
相关推荐
SpringBoot JPA 表关联查询实例
本篇文章主要介绍了SpringBoot JPA 表关联查询实例,使用JPA原生的findBy语句实现,具有一定的参考价值,有兴趣的可以了解一下。
在Spring Boot中使用Spring-data-jpa实现分页查询
如何使用jpa进行多条件查询以及查询列表分页呢?下面我将介绍两种多条件查询方式。具体实例代码大家参考下本文吧
jpa-onetomany-springboot-maven-mysql:使用Spring Boot,Maven和MySQL的JPA一对多关系映射示例
Spring Boot,Maven和MySQL的JPA一对多关系示例 指南 先决条件 JDK 1.8或更高版本 Maven 3或更高版本 MySQL 5.6或更高版本 叠放 Spring Data JPA Sprint Boot MySQL 跑 mvn spring-boot:run
spring boot 整合JPA及使用方法
关于spring boot整合JPA的教程 以及使用方法 讲解了实体类映射表结构 接口的使用
spring boot与cassandra集成,使用JPA方式。
spring boot与cassandra集成,使用JPA方式。


