Hibernate 连接和限制多个表
Hibernate Join and Restrictions on multiple tables
我有三个表要使用 Hibernate 或 JPA 查询...
表格结构
CREATE TABLE `product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`insert_date` datetime NOT NULL,
`last_update` datetime NOT NULL,
...
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;
CREATE TABLE `language` (
`language_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
...
PRIMARY KEY (`language_id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `product_description` (
`product_id` int(11) unsigned NOT NULL,
`language_id` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`tag` text NOT NULL,
`meta_title` varchar(255) NOT NULL,
`meta_description` varchar(255) NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`,`language_id`),
KEY `name` (`name`),
KEY `product_language_idx` (`language_id`),
CONSTRAINT `product_description` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `product_language` FOREIGN KEY (`language_id`) REFERENCES `language` (`language_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Product <OneToMany> ProductDescription
Language <ManyToOne> ProductDescription
假设我的实体如下:
产品实体:
@Entity
@Table(name="product")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="product_id", unique=true, nullable=false)
private int productId;
....
//bi-directional many-to-one association to ProductDescription
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
private List<ProductDescription> productDescriptions;
语言实体:
@Entity
@Table(name="language")
@NamedQuery(name="Language.findAll", query="SELECT l FROM Language l")
public class Language implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="language_id", unique=true, nullable=false)
private int languageId;
...
//bi-directional many-to-one association to ProductDescription
@ManyToOne(fetch=FetchType.LAZY)
//@JoinColumn(name="language_id", referencedColumnName="id",nullable=false, insertable=false, updatable=false)
private ProductDescription productDescription;
ProductDescription 实体(id 是 class ProductDescriptionPK 中的复合键):
@Entity
@Table(name="product_description")
@NamedQuery(name="ProductDescription.findAll", query="SELECT p FROM ProductDescription p")
public class ProductDescription implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ProductDescriptionPK id;
//bi-directional many-to-one association to Language
@OneToMany(mappedBy="productDescription")
private List<Language> languages;
//bi-directional many-to-one association to Product
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="product_id", nullable=false, insertable=false, updatable=false)
private Product product;
所以我必须执行一个查询作为示例,使用产品和 product_description 之间的连接,其中语言是 1:
SELECT p.product_id, pd.description
FROM product p
INNER JOIN product_description pd
USING ( product_id )
INNER JOIN language
USING ( language_id )
WHERE language_id = 1
首先,我将 Hibernate 与 Criteria Query 结合使用:
Criteria criteria = getSession().createCriteria(Product.class,"p");
criteria.createAlias("p.productDescriptions", "pd");
criteria.createAlias("pd.languages", "l");
criteria.add(Restrictions.eq("l.languageId", new Integer(1)));
result = criteria.list();
但是该代码检索到此错误:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Unknown column 'l2_.productDescription_language_id' in 'field list'
我的问题是什么?我怎样才能执行这样的查询?
非常感谢!
我不完全理解你的数据模型。
我认为 Language
和 ProductDescription
之间的关系从 Language
的角度来看应该是一对多的,但抛开这一点 ...
已更新:
实际上,Hibernate 没有使用您在上面指出的注释正确映射关系。它试图在 table language
中映射奇怪的 ManyToOne 关系,但找不到这些字段:productDescription_language_id
和 productDescription_product_id
.
我认为 table 的正确映射是:
语言实体
@Entity
public class Language {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="language_id", unique=true, nullable=false)
private Long languageId;
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="language")
private List<ProductDescription> productDescriptions =
new ArrayList<ProductDescription>();
// Other fields + getters and setters
}
产品描述实体
@Entity
@Table(name="product_description")
public class ProductDescription {
@Embeddable
public static class ProductDescriptionPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "product_id")
protected Long productId;
@Column(name = "language_id")
protected Long languageId;
public ProductDescriptionPK() {
}
public ProductDescriptionPK(Long productId, Long languageId) {
super();
this.productId = productId;
this.languageId = languageId;
}
}
@EmbeddedId
private ProductDescriptionPK id;
private String description;
@ManyToOne
@JoinColumn(name="language_id", nullable=false, insertable=false, updatable=false)
private Language language;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="product_id", nullable=false, insertable=false, updatable=false)
private Product product;
// Other fields + getters and setters
}
这是一个工作示例,说明如何使用您在问题中声明的实体定义的实体与 JPQL 链接。
EntityManager em = emf.createEntityManager();
// [UPDATED] QUERY
String jpql = "SELECT p.id, pd.description FROM Product p "
+ "JOIN p.productDescriptions pd "
+ "JOIN pd.language l WHERE l.language_id = :idLanguage)";
Query query = newEm.createQuery(jpql);
query.setParameter("idLanguage", new Long(1));
List<Object> resultList = query.getResultList();
System.out.println( resultList.size() + " product(s) found:" );
for (Object singleResult : resultList) {
Object[] singleRow = (Object[]) singleResult;
System.out.println(singleRow[0] + " " + singleRow[1]);
}
该代码生成此 SQL 查询 [已更新]
select
product0_.product_id as col_0_0_,
productdes1_.description as col_1_0_
from
Product product0_
inner join
product_description productdes1_
on product0_.product_id=productdes1_.product_id
inner join
Language language2_
on productdes1_.language_id=language2_.language_id
where
language2_.language_id=?
我一直在阅读有关该主题的一些文章和书籍,并且使用带有 where 子句的左连接提取是无效的。引用 Gavin King 等人的 "Java Persistence with Hibernate":
“查询 left join fetch i.bids b where b.amount
... 无效。你不能说,"Load the Item
instances and initializes their bids collections, but only with Bid
instances that have a certain amount"
希望这对您有所帮助。
我有三个表要使用 Hibernate 或 JPA 查询...
表格结构
CREATE TABLE `product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`insert_date` datetime NOT NULL,
`last_update` datetime NOT NULL,
...
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;
CREATE TABLE `language` (
`language_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
...
PRIMARY KEY (`language_id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `product_description` (
`product_id` int(11) unsigned NOT NULL,
`language_id` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`tag` text NOT NULL,
`meta_title` varchar(255) NOT NULL,
`meta_description` varchar(255) NOT NULL,
`meta_keyword` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`,`language_id`),
KEY `name` (`name`),
KEY `product_language_idx` (`language_id`),
CONSTRAINT `product_description` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `product_language` FOREIGN KEY (`language_id`) REFERENCES `language` (`language_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Product <OneToMany> ProductDescription
Language <ManyToOne> ProductDescription
假设我的实体如下:
产品实体:
@Entity
@Table(name="product")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="product_id", unique=true, nullable=false)
private int productId;
....
//bi-directional many-to-one association to ProductDescription
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
private List<ProductDescription> productDescriptions;
语言实体:
@Entity
@Table(name="language")
@NamedQuery(name="Language.findAll", query="SELECT l FROM Language l")
public class Language implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="language_id", unique=true, nullable=false)
private int languageId;
...
//bi-directional many-to-one association to ProductDescription
@ManyToOne(fetch=FetchType.LAZY)
//@JoinColumn(name="language_id", referencedColumnName="id",nullable=false, insertable=false, updatable=false)
private ProductDescription productDescription;
ProductDescription 实体(id 是 class ProductDescriptionPK 中的复合键):
@Entity
@Table(name="product_description")
@NamedQuery(name="ProductDescription.findAll", query="SELECT p FROM ProductDescription p")
public class ProductDescription implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ProductDescriptionPK id;
//bi-directional many-to-one association to Language
@OneToMany(mappedBy="productDescription")
private List<Language> languages;
//bi-directional many-to-one association to Product
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="product_id", nullable=false, insertable=false, updatable=false)
private Product product;
所以我必须执行一个查询作为示例,使用产品和 product_description 之间的连接,其中语言是 1:
SELECT p.product_id, pd.description
FROM product p
INNER JOIN product_description pd
USING ( product_id )
INNER JOIN language
USING ( language_id )
WHERE language_id = 1
首先,我将 Hibernate 与 Criteria Query 结合使用:
Criteria criteria = getSession().createCriteria(Product.class,"p");
criteria.createAlias("p.productDescriptions", "pd");
criteria.createAlias("pd.languages", "l");
criteria.add(Restrictions.eq("l.languageId", new Integer(1)));
result = criteria.list();
但是该代码检索到此错误:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'l2_.productDescription_language_id' in 'field list'
我的问题是什么?我怎样才能执行这样的查询?
非常感谢!
我不完全理解你的数据模型。
我认为 Language
和 ProductDescription
之间的关系从 Language
的角度来看应该是一对多的,但抛开这一点 ...
已更新:
实际上,Hibernate 没有使用您在上面指出的注释正确映射关系。它试图在 table language
中映射奇怪的 ManyToOne 关系,但找不到这些字段:productDescription_language_id
和 productDescription_product_id
.
我认为 table 的正确映射是:
语言实体
@Entity
public class Language {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="language_id", unique=true, nullable=false)
private Long languageId;
private String name;
@OneToMany(fetch=FetchType.LAZY, mappedBy="language")
private List<ProductDescription> productDescriptions =
new ArrayList<ProductDescription>();
// Other fields + getters and setters
}
产品描述实体
@Entity
@Table(name="product_description")
public class ProductDescription {
@Embeddable
public static class ProductDescriptionPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "product_id")
protected Long productId;
@Column(name = "language_id")
protected Long languageId;
public ProductDescriptionPK() {
}
public ProductDescriptionPK(Long productId, Long languageId) {
super();
this.productId = productId;
this.languageId = languageId;
}
}
@EmbeddedId
private ProductDescriptionPK id;
private String description;
@ManyToOne
@JoinColumn(name="language_id", nullable=false, insertable=false, updatable=false)
private Language language;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="product_id", nullable=false, insertable=false, updatable=false)
private Product product;
// Other fields + getters and setters
}
这是一个工作示例,说明如何使用您在问题中声明的实体定义的实体与 JPQL 链接。
EntityManager em = emf.createEntityManager();
// [UPDATED] QUERY
String jpql = "SELECT p.id, pd.description FROM Product p "
+ "JOIN p.productDescriptions pd "
+ "JOIN pd.language l WHERE l.language_id = :idLanguage)";
Query query = newEm.createQuery(jpql);
query.setParameter("idLanguage", new Long(1));
List<Object> resultList = query.getResultList();
System.out.println( resultList.size() + " product(s) found:" );
for (Object singleResult : resultList) {
Object[] singleRow = (Object[]) singleResult;
System.out.println(singleRow[0] + " " + singleRow[1]);
}
该代码生成此 SQL 查询 [已更新]
select
product0_.product_id as col_0_0_,
productdes1_.description as col_1_0_
from
Product product0_
inner join
product_description productdes1_
on product0_.product_id=productdes1_.product_id
inner join
Language language2_
on productdes1_.language_id=language2_.language_id
where
language2_.language_id=?
我一直在阅读有关该主题的一些文章和书籍,并且使用带有 where 子句的左连接提取是无效的。引用 Gavin King 等人的 "Java Persistence with Hibernate":
“查询 left join fetch i.bids b where b.amount
... 无效。你不能说,"Load the Item
instances and initializes their bids collections, but only with Bid
instances that have a certain amount"
希望这对您有所帮助。