在Hibernate中从两个表中选择
首先,我真的很抱歉问这样一个基本的问题,我知道这会让人恼火,但我想知道我是否朝着错误的方向走了。当然,我将逐步阅读文档,但现在我只想解决这个问题 我想从两个具有多对一关联的表中选择值(每个类别可能适合不同的计算机) 以下是自动生成的POJO类:在Hibernate中从两个表中选择,hibernate,jpa,Hibernate,Jpa,首先,我真的很抱歉问这样一个基本的问题,我知道这会让人恼火,但我想知道我是否朝着错误的方向走了。当然,我将逐步阅读文档,但现在我只想解决这个问题 我想从两个具有多对一关联的表中选择值(每个类别可能适合不同的计算机) 以下是自动生成的POJO类: @Table(name="computers", catalog="dbname") public class Computers implements java.io.Serializable { private Integer com
@Table(name="computers", catalog="dbname")
public class Computers implements java.io.Serializable {
private Integer computerId;
private Categories categories;
private String computerName;
public Computers() {
}
public Computers(Categories categories, String computerName) {
this.categories = categories;
this.computerName = computerName;
}
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name="computerId", unique=true, nullable=false)
public Integer getComputerId() {
return this.computerId;
}
public void setComputerId(Integer computerId) {
this.computerId = computerId;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="categoryId", nullable=false)
public Categories getCategories() {
return this.categories;
}
public void setCategories(Categories categories) {
this.categories = categories;
}
@Column(name="computerName", nullable=false)
public String getComputerName() {
return this.computerName;
}
public void setComputerName(String computerName) {
this.computerName = computerName;
}
}
@Entity
@Table(name="categories"
,catalog="dbname"
)
public class Categories implements java.io.Serializable {
private Integer categoryId;
private String categoryName;
private Set<Computers> computerss = new HashSet<Computers>(0);
private Set<Customers> customerss = new HashSet<Customers>(0);
public Categories() {
}
public Categories(String categoryName) {
this.categoryName = categoryName;
}
public Categories(String categoryName, Set<Computers> computerss, Set<Customers> customerss) {
this.categoryName = categoryName;
this.computerss = computerss;
this.customerss = customerss;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="categoryId", unique=true, nullable=false)
public Integer getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
@Column(name="categoryName", nullable=false)
public String getCategoryName() {
return this.categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categories")
public Set<Computers> getComputerss() {
return this.computerss;
}
public void setComputerss(Set<Computers> computerss) {
this.computerss = computerss;
}
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categories")
public Set<Customers> getCustomerss() {
return this.customerss;
}
public void setCustomerses(Set<Customers> customerss) {
this.customerss = customerss;
}
@Table(name=“computers”,catalog=“dbname”)
公共类计算机实现java.io.Serializable{
专用整数计算机ID;
私人类别;
私有字符串名称;
公共计算机(){
}
公共计算机(类别,字符串computerName){
这个。类别=类别;
this.computerName=computerName;
}
@身份证
@生成值(策略=标识)
@列(name=“computerId”,unique=true,nullable=false)
公共整数getComputerId(){
返回此.computerId;
}
public void setComputerId(整数computerId){
this.computerId=computerId;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“categoryId”,nullable=false)
公共类别getCategories(){
返回此项。类别;
}
公共无效集合类别(类别){
这个。类别=类别;
}
@列(name=“computerName”,nullable=false)
公共字符串getComputerName(){
返回此.computerName;
}
public void setComputerName(字符串computerName){
this.computerName=computerName;
}
}
@实体
@表(name=“categories”
,catalog=“dbname”
)
公共类类别实现java.io.Serializable{
私有整数类别ID;
私有字符串categoryName;
专用集Computers=新哈希集(0);
私有集customerss=新哈希集(0);
公共类别(){
}
公共类别(字符串categoryName){
this.categoryName=categoryName;
}
公共类别(字符串类别名称、集合计算机、集合客户){
this.categoryName=categoryName;
this.computerss=computerss;
this.customerss=customerss;
}
@Id@GeneratedValue(策略=标识)
@列(name=“categoryId”,unique=true,nullable=false)
公共整数getCategoryId(){
返回此.categoryId;
}
public void setCategoryId(整型categoryId){
this.categoryId=categoryId;
}
@列(name=“categoryName”,nullable=false)
公共字符串getCategoryName(){
返回this.categoryName;
}
public void setCategoryName(字符串categoryName){
this.categoryName=categoryName;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=“categories”)
公共集GetComputers(){
把这个还给我;
}
公共无效setComputerss(setComputerss){
this.computerss=computerss;
}
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=“categories”)
公共集getCustomerss(){
返回此文件。customerss;
}
public void setcustomerss(Set customerss){
this.customerss=customerss;
}
如果我使用HQL查询从计算机中选择值
(我将它们放在列表中),我可以看到${computer.computerName}
,但没有${computer.categories.categoryName}
出现。我希望每个categoryName
都由id选择。所有类别都需要与名称一起显示。此任务的SQL查询编写起来并不困难,但我想使用Hibernate,目前我还不太了解。我以为我需要的只是类中表示的映射。我的简单来自计算机
这样的选择是错误的?我没有收到任何错误,这会使我更容易理解我的错误。任何帮助都将不胜感激。除了使用@ManyToOne(fetch=FetchType.EAGER)全局配置“计算机到类别”的获取计划外
在映射元数据中,您还可以将此关系保持为惰性关系,并在HQL中使用fetch join急切地获取特定用例的类别
from Computers computer join fetch computer.categories
然后,返回的计算机实例的类别将被完全初始化。如果您将@ManyToOne
注释上的fetch类型从FetchType.LAZY
更改为FetchType.EAGER
,会发生什么情况?这可能不是您问题的根源,但这就是我在类似情况下得出的结论。感谢他回答说,我迫不及待地想尝试这个选项。可惜我只有在星期天才有机会尝试。但我一定会检查FetchType。EAGER
可以做到这一点。你的HQL查询像这个一样简单吗?一个omany而不是多个omytone,但在其他方面都很相似。这真的很有希望,谢谢你。如果它能工作,那就有点奇怪了,只要在这段代码中调用getter,我认为延迟加载也应该在这里起作用。无论如何,我会写下它是否起作用。你需要检查你的数据…例如,你正在查询的sql数据库,它是什么样子的?它在Categories表中有条目根据这个映射引用Computers表中的行吗?谢谢,我很抱歉I’我试试看。我想它可能有用。
from Computers computer join fetch computer.categories