我使用Hibernate Annotations来映射表,但我想转移到xml映射文件(* hbm.xml),我遇到了一些麻烦。我有4个表(用户,汽车,租赁,user_car)和3个类(用户,汽车,租赁)
用户
@Entity
@Table(name = "USERS")
@SuppressWarnings("serial")
public class User implements Serializable {
private Long id;
private List<Car> cars;
@Id
@GeneratedValue
@Column(name = "ID")
public Long getId() {
return id;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USER_CAR", joinColumns = { @JoinColumn(name = "CAR_ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID", referencedColumnName = "ID") })
public List<Car> getCars() {
return cars;
}
...
}
汽车
@Entity
@Table(name = "CARS")
@SuppressWarnings("serial")
public class Car implements Serializable {
private Long id;
private User owner;
@Id
@GeneratedValue
@Column(name = "ID")
public Long getId() {
return id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "USER_CAR", joinColumns = { @JoinColumn(name = "CAR_ID", referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
public User getOwner() {
return owner;
}
...
}
租赁
@Entity
@Table(name = "RENTALS")
@SuppressWarnings("serial")
public class Rental implements Serializable {
private Long id;
private Car car;
private User user;
private Date date;
@Id
@GeneratedValue
@Column(name = "ID")
public Long getId() {
return id;
}
@ManyToOne
@JoinColumn(name = "CAR_ID")
public Car getCar() {
return car;
}
@ManyToOne
@JoinColumn(name = "USER_ID")
public User getUser() {
return user;
}
@Column(name = "RENT_DATE", nullable=false)
@Temporal(TemporalType.DATE)
public Date getDate() {
return date;
}
...
}
Eclipse生成了映射文件,并且我做了一些改进
汽车
<hibernate-mapping>
<class name="com.bontade.mvc.models.Car" table="CARS">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="owner" class="com.bontade.mvc.models.User" fetch="join">
<column name="USER_CAR" />
</many-to-one>
</class>
</hibernate-mapping>
用户
<hibernate-mapping>
<class name="com.bontade.mvc.models.User" table="USERS">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" not-null="true" length="100" type="java.lang.String">
<column name="NAME" />
</property>
<list name="cars" inverse="false" table="CAR" lazy="true">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<one-to-many class="com.bontade.mvc.models.Car" />
</list>
</class>
</hibernate-mapping>
租赁
<hibernate-mapping>
<class name="com.bontade.mvc.models.Rental" table="RENTALS">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="car" class="com.bontade.mvc.models.Car" fetch="join">
<column name="CAR_ID" />
</many-to-one>
<many-to-one name="user" class="com.bontade.mvc.models.User" fetch="join">
<column name="USER_ID" />
</many-to-one>
<property name="date" type="java.util.Date">
<column name="RENT_DATE" />
</property>
</class>
</hibernate-mapping>
但是,我不知道如何声明和连接“代理”表USER_CAR。
答案 0 :(得分:2)
这样的话,请参阅8.5. Bidirectional associations with join tables:
<class name="com.bontade.mvc.models.Car" table="CARS">
...
<join table="USER_CAR"
inverse="true"
optional="true">
<key column="CAR_ID"/>
<many-to-one name="owner"
column="USER_ID"
not-null="true"/>
</join>
</class>
<class name="com.bontade.mvc.models.User" table="USERS">
...
<list name="cars" inverse="false" table="USER_CAR" lazy="true">
<key>
<column name="CAR_ID" />
</key>
<many-to-many column = "USER_ID" unique = "true"
class="com.bontade.mvc.models.Car" />
</list>
</class>
答案 1 :(得分:1)
这样的内容应该放在用户定义中。
<join table="USER_CAR" optional="true">
<key column="user_id" unique="true"/>
<many-to-one name="car" column="car_id" not-null="true"/>
</join>