-->
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: Mapping relationship with partial composite key column
PostPosted: Wed Mar 04, 2009 2:25 pm 
Newbie

Joined: Tue Mar 25, 2008 2:04 pm
Posts: 4
Working on Hibernate 3.2.4ga, calling Oracle 10. It is likely I am not setting this up properly; however, here is my case.

I have 2 legacy database tables. The first table is called "TITLES". It contains a fk with the column name "TITL_BOOK_CAT". The second table is called "BOOKCAT_ACCESS" and contains a non-generated/non-sequential composite pk of columns named "BCCA_BOOK_CATEGORY" and "BCCA_ACCESS_LVL".

My goal is to setup a relationship between the Titles and BookCatAccess entities such that each Title could have one to many BookCatAccess entities. I have tried various ways to map this relationship; however, I have not been able to get the results I expected.

Below is the source code:

@Entity
@Table(name="TITLES")
public class Titles {
...
@Column(name="TITL_BOOK_CAT")
private String titleBookCat;

...<getters and setters for titleBookCat>...

@OneToMany
@JoinColumn(name="BCCA_BOOK_CATEGORY", referencedColumnName="TITL_BOOK_CAT")
private List<BookCatAccess> bookCategoryAccessList;

...<getters and setters for bookCategoryAccessList>...

}

@Entity
@Table(name="BOOKCAT_ACCESS")
public class BookCatAccess {

@EmbeddedId
private BookCatAccessId id;

...<getters and setters for id>...

@Embeddable
public class BookCatAccessId implements Serializable {
@Column(name="BCCA_BOOK_CATEGORY")
private String bookCategory;

@Column(name="BCCA_ACCESS_LVL")
private Long accessLevel;
}
}

I do not get SQL back as this call results in a ClassCastException.

Any direction or advice would be helpful.

Mike

Hopefully I have described the problem well enough. Any advice or direction would be helpful.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 6:02 pm 
Newbie

Joined: Tue Mar 25, 2008 2:04 pm
Posts: 4
Made some changes to see what I could do here.

In the BookCatAccess class, I changed to use @IdClass. Got a little farther than before.

@Entity
@Table(name="BOOKCAT_ACCESS")
@IdClass(BookCatAccess.BookCatAccessId.class)
public class BookCatAccess {

@Id
private String bookCategory;
@Id
private Long accessLevel;

...

@Embeddable
public class BookCatAccessId implements Serializable {

@Column(name="BCCA_BOOK_CATEGORY")
private String bookCategory;

@Column(name="BCCA_ACCESS_LVL")
private Long accessLevel;
}

My titles class now has the following for it's mapping (I removed the referenced name):

@OneToMany()
@JoinColumn(name="BCCA_BOOK_CATEGORY")
private List<BookCatAccess> bookCategoryAccessList;

Interestingly, now I have a query coming back properly; however, it using the value of the primary key on the Titles entity rather than the join column I have specified. In other words, it should bind to 'V' rather than '3434610'.

select bookcatego0_.BCCA_BOOK_CATEGORY as BCCA2_1_,
bookcatego0_.BCCA_ACCESS_LVL as BCCA1_1_,
bookcatego0_.BCCA_ACCESS_LVL as BCCA1_11_0_,
bookcatego0_.BCCA_BOOK_CATEGORY as BCCA2_11_0_,
bookcatego0_.BCCA_QTY_LIMIT as BCCA3_11_0_,
bookcatego0_.BCCA_SHOW_PRICE_FLAG as BCCA4_11_0_
from BOOKCAT_ACCESS bookcatego0_
where bookcatego0_.BCCA_BOOK_CATEGORY='3434610'

So, next I changed the @JoinColumn above my Titles list to the following:

@JoinColumn(name="BCCA_BOOK_CATEGORY", referencedColumnName="TITL_BOOK_CAT")

Now, I am getting a new error. Its a ClassCastException:


[junit] Error: com.fb.model.Titles
[junit] java.lang.ClassCastException: com.fb.model.Titles
[junit] at org.hibernate.type.CollectionType.getKeyOfOwner(CollectionType.java:348)
[junit] at org.hibernate.type.CollectionType.resolve(CollectionType.java:368)
[junit] at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
[junit] at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
[junit] at org.hibernate.loader.Loader.doQuery(Loader.java:729)
[junit] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
[junit] at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
[junit] at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
[junit] at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
[junit] at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
[junit] at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
[junit] at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
[junit] at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
[junit] at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
[junit] at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
[junit] at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
[junit] at com.fb.model.Titles$$EnhancerByCGLIB$$602e545c.getTitleCatalogNumber(<generated>)


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.