Hibernate ManyToMany加入

时间:2015-02-26 11:55:26

标签: java spring hibernate

我是Hibernate的新手,我在Spring中使用它。我有以下表格:

@Entity
@Configurable
public class Location {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long locationid;

    private String locationName;

    @ManyToOne
    private Site site;

//getters setters skipped
}

@Entity
@Configurable
public class Site {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long siteid;

    private String siteName;

    @ManyToOne
    private Country country;
    //getters setters skipped
    }

@Entity
@Configurable
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long countryid;

    private String countryName;
    @ManyToOne
    private Region region;
    //getters setters skipped
}

@Entity
@Configurable
public class Region {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long regionid;

    private String regionName;
    //getters setters skipped
}

public class Assets {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long assetId;


    @ManyToOne
    private Location location;
    //getters setters skipped
}

我想根据地区提取所有资产。我该怎么做?

如何使用Region - >之间的关系?国家 - >网站 - >位置并拉相关记录?如何在不影响性能的情况下实现?

或者我应该重新设计表格?

1 个答案:

答案 0 :(得分:0)

您可以通过Assets类中的@NamedQuery完成它:
 select a from Asserts a where a.location.site.contry.regin.reginName = :reginName

希望帮助。