首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring数据JPA JDBC语句疯狂

Spring数据JPA JDBC语句疯狂
EN

Stack Overflow用户
提问于 2018-03-18 19:04:52
回答 1查看 634关注 0票数 0

我有一个简单的实体:

代码语言:javascript
复制
@Entity
@Table(name = "dish_type")
class DishType : Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long = -1
    var name: String? = null
    var description: String? = null
    @OneToMany(mappedBy = "dishType")
    var dishTypeLocales: List<DishTypeLocale>? = null
}

@Entity
@Table(name = "dish_type_locale")
class DishTypeLocale : Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long = -1

    @Enumerated(EnumType.STRING)
    var locale: LocaleString? = null
    var value: String? = null

    @ManyToOne
    @JoinColumn(name = "dish_type_id")
    var dishType: DishType? = null
}

DAO:

代码语言:javascript
复制
interface DishTypeService {
    fun findAll(withLocale: Boolean): List<DishTypeDto>
}

@Service
@Transactional
open class DishTypeServiceImpl(private val dishTypeRepository: DishTypeRepository) : DishTypeService {

    override fun findAll(withLocale: Boolean): List<DishTypeDto> {
        return this.dishTypeRepository.findAll().map { DishTypeDto(it, withLocale) }
    }

}

@Repository
interface DishTypeRepository : JpaRepository<DishType, Long>

DTO:

代码语言:javascript
复制
class DishTypeDto {
    var id: Long = -1
    var description: String? = null
    var locale: List<DefaultLocalizationDto>? = null

    constructor()

    constructor(dishType: DishType, withLocale: Boolean) {
        this.id = dishType.id
        this.description = dishType.description
        if (withLocale) {
            this.locale = dishType.dishTypeLocales?.map { DefaultLocalizationDto(it) }
        }
    }

    override fun toString(): String {
        return "DishTypeDto{" +
                "id=" + id +
                ", description='" + description + '\'' +
                '}'
    }
}


class DefaultLocalizationDto {
    var locale: Int? = null
    var name: String? = null
    var description: String? = null

    constructor()

    constructor(locale: DishTypeLocale) {
        this.locale = locale.locale?.code
        this.name = locale.value
    }

    override fun toString(): String = "DefaultLocalizationDto(locale=$locale, name=$name, description=$description)"
}

对于DishTypeService.findAll(false),我们有1条语句给DB:

代码语言:javascript
复制
Session Metrics {
    6789040 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    146499 nanoseconds spent preparing 1 JDBC statements;
    3929488 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    43774 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}

对于DishTypeService.findAll(true)语句,== table.size (在我的例子中是282):

代码语言:javascript
复制
Session Metrics {
    11570010 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    4531164 nanoseconds spent preparing 282 JDBC statements;
    60280410 nanoseconds spent executing 282 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    60464 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}

如何告诉Spring,获取1中的所有数据(ok mb2-3语句)?

代码语言:javascript
复制
@OneToMany(mappedBy = "dishType", fetch = FetchType.EAGER)
var dishTypeLocales: List<DishTypeLocale>? = null

好心帮不上忙

我知道,我可以从dishTypeLocales中删除DishType,在两个单独的方法中得到所有的dishTypes然后是所有的dishTypeLocales,然后在代码中映射它们,但是mb有更好的方法吗?

我在用:

代码语言:javascript
复制
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>

DB: Postgresql 9.6

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-18 20:20:23

您正在经历关系的延迟加载,这是OneToMany关系的默认行为。

你在这里有四个选择:

  1. 为这些关系设置一个热切的加载程序(我知道你已经尝试过了,但没有成功,但相信我,它是有效的。)你可能只是经历了一个不同的问题)
  2. 使用FETCH JOIN加载实体以及关系。为此,您必须在存储库中使用以下查询(草稿)编写自定义方法:SELECT dt FROM DishType dt JOIN FETCH dt.dishTypeLocales
  3. 使用Spring的特性,取/加载图
  4. 使用预测,因为这是一个只读操作.最好避免在读取数据时使用实体,只有在修改数据时才坚持使用实体。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49351763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档