多个表上的Hibernate Join和Restrictions

时间:2016-05-26 09:25:46

标签: java hibernate hibernate-criteria

我有三个表可以用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 Entity(id是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和product_description之间的连接执行查询作为示例,其中language是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();

但该代码检索此错误:

  

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:   未知栏&#39; l2_.productDescription_language_id&#39;在&#39;字段列表&#39;

我的问题是什么?我该如何执行这样的查询?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我完全不了解您的数据模型。 我认为LanguageProductDescription之间的关系应该从Language的角度来看是一对多的,但是把它放在一边......

<强>已更新

实际上,Hibernate没有使用您在上面指出的注释正确映射关系。它试图在表language中映射奇怪的ManyToOne关系,但找不到这些字段:productDescription_language_idproductDescription_product_id

我认为表格的正确映射是:

语言实体

@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

@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持久性与Hibernate”: “查询left join fetch i.bids b where b.amount ...无效。您无法说,”加载Item个实例并初始化其出价集合,但仅限于Bid个实例一定金额

希望这有帮助。