I have three tables to query with Hibernate or JPA...
Tables structure
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
Assuming my entities are the following:
Product Entity:
@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;
Language Entity:
@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 is a composite key in 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;
So I have to execute a query as sample, using a join between product and product_description, where language is 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
Firstly I have used Hibernate with 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();
But that code retrieves this error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'l2_.productDescription_language_id' in 'field list'
What's my issue? How can I perform queries like this?
Thanks a lot!