本文介绍了Hibernate @ManyToOne映射 - 无需@Id属性设置的自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强迫Hibernate使用 ManyToOne 关系中的其他对象加载我的主对象?这是设置其他值的时刻,除 @Id 属性外。



您可以检查我的回购github上的maven项目,是一个JUnit测试类,我尝试了这种行为。
$ b

Address 是实体类我想坚持数据库,但只有来自 Country 的国家代码。 Country 表中的所有行都是常量,因此不应再次插入 Country 对象,只有 countryId 需要写入。在Hibernate中是否有任何自动化机制,或者我必须在 Address 之前的某个服务事务方法中手动加载 Country 持久性?

解决方案

不,不可能,java不能知道何时 new Country(BE )等于 countryDao.getByCode(BE),因为没有等号,一个由Hibernate管理,另一个是由您管理。

您不会将新的Country(BE)赋予Hibernate,所以它不能同样,你也调用了新的Countru(BE),代码为null,代码 countryDao.getByCode(BE)不为null(它是由您的SQL脚本创建的,现在由Hibernate管理)。



您有两种选择:


  • 将您的测试更改为:

     国家country = countryDao.getByCode(BE); 
    地址=新地址();
    address.setCountry(country);
    addressDao.create(address);
    assertEquals(country.getCountryId(),addressDao.get(address.getAddressId())。getCountry()。getCountryId());

    测试地址是否被正确保存,或者:


  • 像这样创建 CountryProvider :

     公共类CountryProvider {

    @Autowired HbnCountryDao dao;
    地图< String,Country> map = new HashMap< String,Country>();
    $ b $ public Country getByCode(String code){
    if(map.contains(code))return map.get(code);
    国家toRet = dao.getByCode(code);
    map.put(code,toRet);
    返回到收益;




    $ b $ p $让你的所有国家构造函数为private或protected,并且只能由 CountryProvider 访问 Country 。



How can I force Hibernate to load my main object with some other object from ManyToOne relation? That's the moment where some other value is set, other than @Id property.

Can you check my repo with maven project on github, HbnAddressDaoTest is a JUnit test class where I'm trying this behaviour

Address is the entity class I would like to persist to database but only have country code from Country. All rows in Country table are constants, so Country object shouldn't be inserted again, only countryId need to be written. Is there any automation mechanism in Hibernate for this or do I have to manually load Country in some service transactional method before Address persistence?

解决方案

No, it's not posible, java can't know when new Country("BE") are equal to countryDao.getByCode("BE"), because, there are not equals, one is managed by Hibernate and the other is managed by you.

You don't give the new Country("BE") to Hibernate, so it can not be the same, also, went you invoke the new Countru("BE"), the code is null, and the code of countryDao.getByCode("BE") is not null (it was created by your SQL script and now is managed by Hibernate).

You have two options:

  • Change your test to:

    Country country = countryDao.getByCode("BE");
    Address address = new Address();
    address.setCountry(country);
    addressDao.create(address);
    assertEquals(country.getCountryId(), addressDao.get(address.getAddressId()).getCountry().getCountryId());
    

    To test if the address is correctly persisted, or:

  • Create a CountryProvider like this:

    public class CountryProvider {
    
         @Autowired HbnCountryDao dao;
         Map<String, Country> map = new HashMap<String, Country>();
    
         public Country getByCode(String code) {
             if (map.contains(code)) return map.get(code);
             Country toRet = dao.getByCode(code);
             map.put(code, toRet);
             return toRet;
         }
    }
    

    Make all of your Country constructors private or protected, and only access the Country by the CountryProvider.

这篇关于Hibernate @ManyToOne映射 - 无需@Id属性设置的自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:09