我想使用注释而不是xml。我不明白如何建立联系。这就是我所拥有的:
category.hbm.xml:
//some code
<hibernate-mapping>
<class name="com.logicify.shopcart.logic.Category" table="Category">
<id column="catid" name="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="name" name="name" type="java.lang.String"/>
<property column="description" name="description" type="java.lang.String"/>
<set name="products" table="prodcat" lazy="false">
<key column="catid"/>
<many-to-many column="prodid" class="com.logicify.shopcart.logic.Product"/>
</set>
</class>
</hibernate-mapping>
product.hbm.xml:
//some code
<hibernate-mapping>
<class name="com.logicify.shopcart.logic.Product" table="Product">
<id column="prodid" name="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="name" name="name" type="java.lang.String"/>
<property column="description" name="description" type="java.lang.String"/>
<set name="categories" table="prodcat" lazy="false">
<key column="prodid"/>
<many-to-many column="catid" class="com.logicify.shopcart.logic.Category"/>
</set>
</class>
</hibernate-mapping>
Category.java:
@Entity
@Table(name="CATEGORY")
public class Category {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="catid", updatable=false, nullable=false)
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
private Set products = new HashSet();
@ManyToMany(cascade= {CascadeType.ALL})
@JoinTable(name="products", schema=joinColumns={@JoinColumn(name="catid")}, inverseJoinColumns={@JoinColumn(name="prodid"}))
//then getters and setters
Product.java:
@Entity
@Table(name="PRODUCT")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="prodid")
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
private Set categories = new HashSet();
@ManyToMany(cascade= {CascadeType.ALL})
@JoinTable(name="category", schema=joinColumns={@JoinColumn(name="prodid")}, inverseJoinColumns={@JoinColumn(name="catid"}))
我不明白这种关系。请告诉我怎么做。
过了一段时间我试着写这样的东西:
Product.java
@Entity
@Table(name="PRODUCT")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="prodid")
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Category.class)
@JoinTable(name="category", joinColumns={@JoinColumn(name="prodid")}, inverseJoinColumns={@JoinColumn(name="catid")})
private Set categories = new HashSet();
//getters and setters
Category.java:
@Entity
@Table(name="CATEGORY")
public class Category {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="catid", updatable=false, nullable=false)
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Product.class)
@JoinTable(name="products", joinColumns={@JoinColumn(name="catid")}, inverseJoinColumns={@JoinColumn(name="prodid")})
private Set products = new HashSet();
//getters and setters
现在在控制台中写道:
Hibernate: select this_.prodid as prodid1_1_2_, this_.description as
descript2_1_2_, this_.name as name3_1_2_, categories2_.prodid as
prodid1_1_4_, category3_.catid as catid2_2_4_, category3_.catid as
catid1_0_0_, category3_.description as descript2_0_0_, category3_.name
as name3_0_0_, products4_.catid as catid1_0_5_, product5_.prodid as
prodid2_3_5_, product5_.prodid as prodid1_1_1_, product5_.description
as descript2_1_1_, product5_.name as name3_1_1_ from PRODUCT this_
left outer join category categories2_ on
this_.prodid=categories2_.prodid left outer join CATEGORY category3_
on categories2_.catid=category3_.catid left outer join products
products4_ on category3_.catid=products4_.catid left outer join
PRODUCT product5_ on products4_.prodid=product5_.prodid трав. 11, 2014
6:26:49 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logExceptions WARN: SQL Error: -5501, SQLState: 42501 трав. 11, 2014
6:26:49 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logExceptions ERROR: user lacks privilege or object not found:
PRODUCTS