-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: referencedColumnName
PostPosted: Fri May 02, 2008 4:13 am 
Newbie

Joined: Wed Apr 30, 2008 4:23 am
Posts: 4
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


Top
 Profile  
 
 Post subject: Re: referencedColumnName
PostPosted: Tue May 13, 2008 8:45 am 
Newbie

Joined: Wed Apr 30, 2008 4:23 am
Posts: 4
tbt wrote:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.