gpt4 book ai didi

java - Hibernate ManyToMany 加入

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:10 25 4
gpt4 key购买 nike

我是 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
}

我想根据区域提取所有 Assets 。我该怎么做?

如何利用 Region --> Country --> Site --> Location 之间的关系并拉取相关记录?如何在不影响性能的情况下做到这一点?

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

最佳答案

您可以通过 Assets 类中的 @NamedQuery 来完成它:
从断言 a 中选择一个,其中 a.location.site.contry.regin.reginName = :reginName

希望有帮助。

关于java - Hibernate ManyToMany 加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com