• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anyone know how to construct a one to many mapping with Hibernate / Java?

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using Hibernate 3.4.0.GA. I have two objects mapped to database tables (defined in hibernate.cfg.xml like so):



In the database tables, a row from the VENDOR table (primary key column = VENDOR_ID), could map to zero or many rows in the VENDOR_EMAILS table, which has the corresponding column, VENDOR_ID, which is a foreign key. In my "YouthfulDriverVendor" model class, I defined the one to many (vendor to one or multiple emails) like so ...



However, I'm getting this error ...

Repeated column in mapping for collection: myco.dor.dmv.driver.youthful.model.YouthfulDriverVendor.emails column: VENDOR_ID
org.hibernate.MappingException: Repeated column in mapping for collection: myco.dor.dmv.driver.youthful.model.YouthfulDriverVendor.emails column: VENDOR_ID
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:329)
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:352)
at org.hibernate.mapping.Collection.validate(Collection.java:309)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1139)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
.....

What adjustments do I need to make to fix the error I'm getting? Thanks, - Dave
 
Silvana Donato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@JoinTable(name = "VENDOR_EMAILS",
joinColumns = {
@JoinColumn(name="VENDOR_ID", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="VENDOR_ID")
}


I think that joinColumns and inverseJoinColumns refer to the same table VENDOR_EMAILS, so you are creating 2 columns with the same name.
The inverseJoinColumns could be emails_id

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that got me passed that problem. Was wondering if you had any reaction to the next problem I encountered. Stack trace below. It happened in response to my attempt to get the list (collection) --



Thanks again for the help, - Dave

 
Silvana Donato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this means that you are trying to get a collection that has not actually been retrieved by the db yet.
You can try to set fetch property to fetchtype.eager in the annotation of the collection property.
Something like:

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic