当实体中存在关系时,是否可以仅使用实体的ID而不是从数据库中获取实体? [英] Can I use only Entity's id instead of fetching an entity from DB when there is a relationship in an Entity?

查看:55
本文介绍了当实体中存在关系时,是否可以仅使用实体的ID而不是从数据库中获取实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate中使用Spring Data JPA.

I am using Spring Data JPA with Hibernate.

可以说我定义了以下实体:

Lets say I have the following entity defined:

@Entity
@Table(name = "foods")
public class Food {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "food_id")
    private Long foodId;


    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "food_type_id")
    @NotNull
    private FoodType foodType;
    
    ...
}


@Entity
@Table(name = "food_types")
public class FoodType {
    
    public static final Integer PERISHABLE;
    public static final Integer NON_PERISHABLE;

    @Id
    @Column(name = "food_type")
    private Integer foodTypeId;

    private String name;
    
    ...
}

每次我要创建一个 Food 实体并将其保存到数据库时,当前代码如下:

Every time when I want to create a Food entity and save it to the database, currently code looks like this:

Food food = new Food();
FoodType foodType = foodTypeRepository.findById(FoodType.PERISHABLE); // Call to DB to get Entity
food.setFoodType(foodType);
....

foodRepository.save(food);

如果我们认为FoodType在数据库中是恒定的.我可以这样使用吗:

If we consider FoodType to be constant in the DB. Can I use it like this:

Food food = new Food();
FoodType foodType = new FoodType();
foodType.setFoodTypeId(FoodType.PERISHABLE); // No Call to DB
food.setFoodType(foodType);
....

foodRepository.save(food);

我已经测试过了,是的,我可以那样使用它,休眠将保存 Food 实体,但是有任何缺点,陷阱等...我没看到.

I have tested it and yes I can use it that way, hibernate will save the Food entity, but are there any downsides, pitfalls, etc... I am not seeing.

PS.这只是一个简单的示例,说明了这个想法,它是旧的旧项目的一部分,我无法修改该项目以从DB中删除常量,而改用枚举.

PS. This is just a simple example illustrating the idea, it is part of old legacy project which I cannot modify to remove constant from DB, and use an enum instead.

推荐答案

为避免对数据库的额外调用,您应该使用:

To avoid extra call to DB you should use:

FoodType foodType = foodTypeRepository.getOne(FoodType.PERISHABLE); 

在后台,它调用 EntityManager.getReference 来获取对实体的引用而不必加载其数据,而不是 foodTypeRepository.findById EntityManager.find 并获得实体及其数据的代码.

under the hood it calls EntityManager.getReference that obtain a reference to an entity without having to load its data as opposed to the foodTypeRepository.findById that lead to call EntityManager.find that obtain an entity along with its data.

另请参见本节休眠文档.

P.S.您不能使用:

P.S. You can not use:

Food food = new Food();
FoodType foodType = new FoodType();
foodType.setFoodTypeId(FoodType.PERISHABLE);

在这种情况下,休眠模式将 foodType 视为 transient 实体(不与

as in this case hibernate consider foodType as a transient entity (not associated with a persistence context) and will try to save it as a new record if you have a proper cascading on your @ManyToOne association.

P.S.S.正如

P.S.S. As it's mentioned in the documentation the method JpaRepository#getOne(ID) is deprecated and you should use JpaRepository#getById(ID) instead.

这篇关于当实体中存在关系时,是否可以仅使用实体的ID而不是从数据库中获取实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆