简体   繁体   English

Hibernate OneToOne表联接

[英]Hibernate OneToOne Table join

Learning Hibernate. 学习休眠。 I have the following classes User, Region, Country as follows 我有以下类别的用户,地区和国家,如下所示

     public class User {

     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;
     @Column(name = "first_name")
     Private String firstName;
     @Column(name = "last_name")
     private String lastName;
     @OneToOne(fetch = FetchType.EAGER)
     @JoinTable(name = "user_country_region", joinColumns ={@JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name =   "country_id") })
     private Country userCountry;

     @OneToOne(fetch = FetchType.EAGER)
     @JoinTable(name = "user_country_region", joinColumns = {@JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "region_id") })
     private Region userRegion;

     //With its respective Getters and Setters 
   }

    public class Country {

     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;
     @Column(name = "name")
     private String name;

     //With its respective Getters and Setters 
   }

   public class Region {

     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;
     @Column(name = "name")
     private String name;

     //With its respective Getters and Setters 
   }

The problem am facing is hibernate query only returns region and not country. 面临的问题是休眠查询仅返回区域,而不返回国家。 What could be causing this? 是什么原因造成的?

Tried getting country and region values as below 尝试获取以下国家和地区值

    System.out.println("Country: "+user.getCountry().getName());
    System.out.println("Region: "+user.getRegion().getName());

Response from Hibernate Show sql. 来自Hibernate Show sql的响应。 Seems missing country details. 似乎缺少国家/地区详细信息。

    Hibernate: 
    select
       this_.id as id1_3_3_,
       this_.first_name as first_na2_3_3_,
       this_.last_name as last_na3_3_3_,
       region2_.id as id1_8_2_,
       region2_.name as name2_8_2_ 
    from
       user this_ 
    left outer join
       user_country_region this_1_ 
       on this_.id=this_1_.user_id  
    left outer join
    region region2_ 
       on this_1_.region_id=region2_.id 
    where
    this_.id=?

It is an invalid mapping. 这是无效的映射。 I have this error with Hibernate 5 while create the schema by Hibernate. 我在通过Hibernate创建架构时遇到了Hibernate 5错误。

org.hibernate.boot.spi.InFlightMetadataCollector$DuplicateSecondaryTableException: 
Table with that name [user_country_region] already associated with entity

Anyway, if you can use this mapping with your Hibernate version, having such kind of mapping with a join table for two relations is error prone. 无论如何,如果您可以在您的Hibernate版本中使用此映射,则对于带有两个关系的联接表进行此类映射很容易出错。

Just use this mapping to associate User with Country and Region by foreign key columns. 只需使用此映射即可通过外键列将UserCountryRegion相关联。

public class User {

     @OneToOne
     private Country country;

     @OneToOne
     private Region region;

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM