Hibernate version: 3.2.6
Name and version of the database you are using: mysql 5.0
Hi
I'm new to hibernate and i have a bidirectional association mapping between two tables. The code is as follows.
Code:
CREATE TABLE `hotelboards` (
`HotelId` varchar(30) collate latin1_general_ci NOT NULL,
`BoardTypeId` int(11) NOT NULL,
PRIMARY KEY (`HotelId`,`BoardTypeId`)
);
CREATE TABLE `hotelrooms` (
`HotelId` varchar(30) collate latin1_general_ci NOT NULL,
`BoardTypeId` int(11) NOT NULL,
`RoomTypeId` int(11) NOT NULL,
PRIMARY KEY (`HotelId`,`BoardTypeId`,`RoomTypeId`)
);
There is a one to many mapping from hotelboards to hotelrooms.(a single hotel board can have many room types)
The entity classes are as follows
Code:
@Entity
public class HotelBoards implements Serializable
{
...................................
@Id
public HotelBoardCompoundKey getHotelBoardCompoundKey() {
return hotelBoardCompoundKey;
}
@OneToMany(mappedBy = "hotelBoards")
public List<HotelRooms> getRoomTypeList() {
return roomTypeList;
}
...................................
}
Code:
@Embeddable
public class HotelBoardCompoundKey implements Serializable
{
String hotelId;
Integer boardTypeId;
}
Code:
@Entity
public class HotelRooms implements Serializable
{
............................................
@Id
public HotelRoomsCompoundKey getHotelRoomsCompoundKey() {
return hotelRoomsCompoundKey;
}
@ManyToOne
@JoinColumns({@JoinColumn(name = "BoardTypeId")
,@JoinColumn(name = "HotelId")})
public HotelBoards getHotelBoards() {
return hotelBoards;
}
..............................................
}
Code:
@Embeddable
public class HotelRoomsCompoundKey implements Serializable
{
String hotelId;
Integer boardTypeId;
Integer roomTypeId;
}
This code works fine and produces the required output. But since hibernate uses the column order I need to use the referencedColumnName attribute.
So i added the annotation in getHotelBoards() in the HotelRooms class as follows
Code:
@JoinColumns({@JoinColumn(name = "HotelId", referencedColumnName = "hotelId"),
@JoinColumn(name = "BoardTypeId", referencedColumnName = "boardTypeId")})
But then I get the following exception
Code:
org.hibernate.AnnotationException: Column name boardTypeId of HotelBoards
not found in JoinColumns.referencedColumnName
I tried various modifications without any luck. Can someone tell me how to have a bidirectional association between these two classes. How should I modify the above code using 'referencedColumnName'
Thanks