I used Hibernate Annotations for mapping tables, but I wanted move to xml mapping files (*hbm.xml), and I have some troubles. I have 4 tables (users, cars, rentals, user_car) and 3 classes (User, Car, Rental)
User
@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;
}
...
}
Car
@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;
}
...
}
Rental
@Entity
@Table(name = "RENTALS")
@SuppressWarnings("serial")
public class Rental implements 开发者_StackOverflow社区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 generated mapping files, and I've made some improvements
Car
<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>
User
<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>
Rental
<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>
But, I don't know how can I declare and connect "proxy" table USER_CAR.
Something like this, see 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>
Something like this should go in your User definition.
<join table="USER_CAR" optional="true">
<key column="user_id" unique="true"/>
<many-to-one name="car" column="car_id" not-null="true"/>
</join>
精彩评论