[英]JPQL manyToMany unidirectional query
I have been looking the forums and everywhere for a unidirectional manyToMany query.我一直在寻找论坛和任何地方的单向多对多查询。 I find many examples but i can't really adjust them to my needs :(
我找到了很多例子,但我无法真正根据我的需要调整它们:(
I have 2 entity classes (Anime & User)我有 2 个实体类(动漫和用户)
Anime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "anime_id")
private Integer id;
//Other fields etc.
}
User {
@Id
@ValidUsername
@Column(name = "user_id")
private String username;
@ManyToMany()
@JoinTable(name = "users_animes",
joinColumns = @JoinColumn(name = "anime_id", referencedColumnName = "anime_id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"))
private final List<Anime> animes = new ArrayList<>();
}
Anime simply holds the data from the anime.动漫只是保存动漫中的数据。 User holds the username etc of the user and a list of anime that he subscribed to.
用户持有用户的用户名等以及他订阅的动漫列表。
Now i'm trying to find a query that will let me get all the animes in that list.现在我试图找到一个查询,让我获得该列表中的所有动漫。 It's mapped in a table as "users_animes".
它在表中映射为“users_animes”。
Would be a huge help since i'm fairly new to JPQL.将是一个巨大的帮助,因为我对 JPQL 还很陌生。
Thanks!谢谢!
Here's a simple example.这是一个简单的例子。 Let's assume that we have Country Entity, which can have multiple Citizens:
假设我们有 Country Entity,它可以有多个 Citizens:
@Entity
@Table (name = "countries")
public class Country implements Serializable {
@Id
@Column (name = "coun_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column (name = "country_name")
private String countryName;
@ManyToMany (cascade = CascadeType.ALL)
@JoinTable (
name = "citizen_country",
joinColumns = @JoinColumn (name = "country_id", referencedColumnName = "coun_id"),
inverseJoinColumns = @JoinColumn (name = "citizen_id", referencedColumnName = "cit_id")
)
private List<Citizen> citizens;
public Country() {}
public Country(String countryName) {
this.countryName = countryName;
}
//getters and setters
}
And Citizen, which can belong to multiple Countries:和 Citizen,可以属于多个国家:
@Entity
@Table (name = "citizens")
public class Citizen implements Serializable {
@Id
@Column (name = "cit_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column (name = "country_name")
private String citizenName;
public Citizen() {}
public Citizen(String citizenName) {
this.citizenName = citizenName;
}
public void setId(Long id) { this.id = id; }
public Long getId() { return id; }
public void setCitizenName(String citizenName) { this.citizenName = citizenName; }
public String getCitizenName() { return citizenName; }
//getters and setters
}
It's unidirectional, just as you wanted it to be.它是单向的,正如您希望的那样。 Therefore, Citizen Entity is unaware of Country, so you can't get directly information to which Country a certain Citizen belongs.
因此,Citizen Entity 不知道 Country,因此您无法直接获得某个 Citizen 属于哪个 Country 的信息。 However, you can retrieve info about which Citizens belong to a certain Country.
但是,您可以检索有关哪些公民属于某个国家/地区的信息。
Going further, you can populate your tables:更进一步,您可以填充表格:
Citizen citizen1 = new Citizen("Smith");
Citizen citizen2 = new Citizen("Kowalski");
Citizen citizen3 = new Citizen("Muller");
dataAccess.saveCitizen(citizen1);
dataAccess.saveCitizen(citizen2);
dataAccess.saveCitizen(citizen3);
// now let's fetch them from DB, along with their other properties (only id in this case)
citizen1 = dataAccess.getCitizenByName("Smith");
citizen2 = dataAccess.getCitizenByName("Kowalski");
citizen3 = dataAccess.getCitizenByName("Muller");
Country country1 = new Country("Foo");
Country country2 = new Country("Bar");
// create lists to hold citizens for each country
List<Citizen> citizenList1 = new ArrayList();
List<Citizen> citizenList2 = new ArrayList();
// add elements to the lists
citizenList1.add(citizen1);
citizenList1.add(citizen2);
citizenList2.add(citizen2);
citizenList2.add(citizen3);
//assign lists of citizens to each country
country1.setCitizens(citizenList1);
country2.setCitizens(citizenList2);
//save data in DB
dataAccess.saveCountry(country1);
dataAccess.saveCountry(country2);
//fetch list of all persisted countries (associated Citizens will come along)
countries = dataAccess.getAllCountries();
And finally:最后:
@Stateless
public class DataAccess {
@PersistenceContext
EntityManager em;
public void saveCountry(Country country) {
em.persist(country);
}
public void saveCitizen(Citizen citizen) {
em.persist(citizen);
}
public Citizen getCitizenByName(String name) {
Query query = em.createQuery("SELECT c FROM Citizen c WHERE c.citizenName = :name");
query.setParameter("name", name);
return (Citizen) query.getSingleResult();
}
public List<Country> getAllCountries() {
Query query = em.createQuery("SELECT c FROM Country c");
return (List<Country>) query.getResultList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.