实体级别的 hibernate join 的主要要求是什么
What is the primary requirements for hibernate join on entity level
我下面提到了三个实体,Table下面也提到了结构。现在我试图只获取那些购买了特定商品的客户。
我正在尝试这个无效的查询并面临错误:org.hibernate.hql.internal.ast.ErrorCounter - 预期加入的路径! - 请可能的正确答案。
SELECT customer FROM Customer customer JOIN CustomerItemMapping cim on customer.customerId=cim.customerId where cim.item.itemId in (:itemIdList);
@Entity @Cacheable
@Table(name = "customer_item_mapping")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CustomerItemMapping implements Serializable {
private static final long serialVersionUID = 3500101963230957017L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "mapping_id", unique = true, nullable = false)
private Integer mappingId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_id", nullable = false)
private Item item;
@Column(name = "customer_id", nullable = false)
private Integer customerId;
}
@Entity @Cacheable
@Table(name = "customer")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Customer implements Serializable{
private static final long serialVersionUID = 3886876059389214345L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "customer_id", unique = true, nullable = false)
private Integer customerId;
@JoinColumn(name = "name", nullable = false)
private String customerName;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "customer_item_mapping", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "item_id"))
private Set<Item> itemSet;
}
@Entity @Cacheable
@Table(name = "item")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Item implements Serializable{
private static final long serialVersionUID = 3886876059389214345L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "item_id", unique = true, nullable = false)
private Integer itemId;
@JoinColumn(name = "name", nullable = false)
private String name;
}
CREATE TABLE customer_item_mapping ( mapping_id int(11) unsigned NOT NULL AUTO_INCREMENT, item_id int(100) NOT NULL, customer_id int(11) NOT NULL, PRIMARY KEY (mapping_id))
CREATE TABLE customer ( customer_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (customer_id) )
CREATE TABLE item ( item_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (item_id) )
SELECT distinct customer FROM Customer customer, CustomerItemMapping cim
where customer.customerId = cim.customerId and cim.item.itemId in (:itemIdList)
如您所见,在内部连接非关联实体时可以使用经典的 theta 连接。
我也把cim.tag.itemId
更正为cim.item.itemId
(我不知道tag
代表什么,我在你的任何地方都看不到映射)。
我下面提到了三个实体,Table下面也提到了结构。现在我试图只获取那些购买了特定商品的客户。 我正在尝试这个无效的查询并面临错误:org.hibernate.hql.internal.ast.ErrorCounter - 预期加入的路径! - 请可能的正确答案。
SELECT customer FROM Customer customer JOIN CustomerItemMapping cim on customer.customerId=cim.customerId where cim.item.itemId in (:itemIdList);
@Entity @Cacheable
@Table(name = "customer_item_mapping")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CustomerItemMapping implements Serializable {
private static final long serialVersionUID = 3500101963230957017L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "mapping_id", unique = true, nullable = false)
private Integer mappingId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_id", nullable = false)
private Item item;
@Column(name = "customer_id", nullable = false)
private Integer customerId;
}
@Entity @Cacheable
@Table(name = "customer")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Customer implements Serializable{
private static final long serialVersionUID = 3886876059389214345L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "customer_id", unique = true, nullable = false)
private Integer customerId;
@JoinColumn(name = "name", nullable = false)
private String customerName;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "customer_item_mapping", joinColumns = @JoinColumn(name = "customer_id"), inverseJoinColumns = @JoinColumn(name = "item_id"))
private Set<Item> itemSet;
}
@Entity @Cacheable
@Table(name = "item")
@DynamicInsert(value=true)
@DynamicUpdate(value=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Item implements Serializable{
private static final long serialVersionUID = 3886876059389214345L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "item_id", unique = true, nullable = false)
private Integer itemId;
@JoinColumn(name = "name", nullable = false)
private String name;
}
CREATE TABLE customer_item_mapping ( mapping_id int(11) unsigned NOT NULL AUTO_INCREMENT, item_id int(100) NOT NULL, customer_id int(11) NOT NULL, PRIMARY KEY (mapping_id))
CREATE TABLE customer ( customer_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (customer_id) )
CREATE TABLE item ( item_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, PRIMARY KEY (item_id) )
SELECT distinct customer FROM Customer customer, CustomerItemMapping cim
where customer.customerId = cim.customerId and cim.item.itemId in (:itemIdList)
如您所见,在内部连接非关联实体时可以使用经典的 theta 连接。
我也把cim.tag.itemId
更正为cim.item.itemId
(我不知道tag
代表什么,我在你的任何地方都看不到映射)。