-->
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.  [ 6 posts ] 
Author Message
 Post subject: referencedColumnName of fk column causes MappingException
PostPosted: Tue Nov 14, 2006 11:40 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 1:28 pm
Posts: 39
Location: United States
I'm using Hibernate 3.2.0.GA.

I need to establish a @ManyToOne join between two tables using a composite join on two non-pk columns which are actually @ManyToOne themselves (foreign keys) within their respective classes.

Unless I have something wrong, it appears that the mapping code can not handle a referencedColumnName of a property which is an association in the target table of a @ManyToOne:

Code:
org.hibernate.MappingException: Unable to find column with logical name: VENDOR_ID in org.hibernate.mapping.Table(ItemCost) and its related supertables and secondary tables.
        at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:364)
        at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:88)
        at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:63)
        at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1039)
        at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1207)
        at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:844)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:382)
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
        (many more...)


I've tried to simplify this code to illustrate the objective:

Code:
public class Item {
  int Id;
  Vendor defaultVendor;
  Cost defaultCost;

  @Id getId {
    return id;
  }

  @ManyToOne
  @JoinColumn(name="VENDOR_ID") 
  Vendor defaultVendor getDefaultVendor {
    return defaultVendor;
  }

  @ManyToOne
  @JoinColumns({
    @JoinColumn(name="VENDOR_ID", referencedColumnName="VENDOR_ID", insertable=false, updatable=false),
    @JoinColumn(name="id", referencedColumnName="ITEM_ID", insertable=false, updatable=false)
     })     
  public Cost getDefaultCost() {
    return defaultCost;
  }
}

public class ItemCost {
  int Id;
  Item item;
  Vendor vendor;
  BigDecimal cost;

  @Id getId {
    return id;
  }

  @ManyToOne 
  @JoinColumn(name="ITEM_ID")
  Item item getItem{
    return item
  }


  @ManyToOne
  @JoinColumn(name="VENDOR_ID") 
  Vendor vendor getVendor {
    return vendor
  }

  BigDecimal cost getCost {
    return cost;
  }
}


There will be a unique constraint on the item/vendor combination so there can only be one match in the database at a time, and an index for performance.

I could use a named query within the Item class, however this would not result in a JOIN but a subselect. I believe a JOIN will perform much better during retrieval of Item records.

Using a composite key in the Cost table is not an option for various reasons having to do with our application framework.

I've tried all of the logical names I can think of within the @JoinColumn annotation like vendor.id, VENDOR_ID, vendor_id, vendor, etc. All throw the exception above.

Any advice on how to get this to work would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you wrap that minimal model in a runnable unit test case to JIRA. I'll need to have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject: JIRA case created
PostPosted: Mon Dec 18, 2006 2:04 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 1:28 pm
Posts: 39
Location: United States
http://opensource.atlassian.com/project ... se/ANN-509


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 10:55 pm 
Newbie

Joined: Thu Nov 20, 2008 10:59 pm
Posts: 3
I also have same problem.
Can u post back how to solve this problem?
What was the problem?


Top
 Profile  
 
 Post subject: Re: referencedColumnName of fk column causes MappingException
PostPosted: Wed Oct 06, 2010 4:10 pm 
Newbie

Joined: Wed Oct 06, 2010 3:59 pm
Posts: 2
I am having a similar problem with a OneToOne mapping and referencedColumnName. I get the same exception even though I know the column exists on the table. I have two entities, LocationCategory and OrganizationalUnit. LocationCategory has an instance of OrganzationalUnit and OrganizationalUnit has an instance of LocationCategory. OrganizationalUnit has the foreign key to LocationCategory called locationCategoryId. But I want the OrganizationalUnit to be created when I create the LocationCategory, so I have this mapping in my LocationCategory entity:

@OneToOne(fetch = FetchType.LAZY, cascade = javax.persistence.CascadeType.ALL)
@Cascade(CascadeType.ALL)
@JoinColumn(referencedColumnName="locationCategoryId")
public OrganizationalUnit getOrganizationalUnit() {
return organizationalUnit;
}

And this in my OrganizationalUnit:

@OneToOne(mappedBy="organizationalUnit")
public LocationCategory getUnitLocationCategory() {
return unitLocationCategory;
}

And I get this error on startup:
Unable to find column with logical name: locationCategoryId in org.hibernate.mapping.Table(OrganizationalUnit) and its related supertables and secondary tables

But there is a locationCategoryId on the OrganizationalUnit table. It is a foreign key to the LocationCategory primary key.

This error seems like a bug.

Thanks,

Dave


Top
 Profile  
 
 Post subject: Re: referencedColumnName of fk column causes MappingException
PostPosted: Fri Oct 08, 2010 3:53 pm 
Newbie

Joined: Wed Oct 06, 2010 3:59 pm
Posts: 2
I was having issues with referencedColumnName in the JoinColumn of a OneToOne. I realized that I just needed to ditch the referencedColumnName and move the JoinColumn to the other side of the relationship using the name attribute.

The examples I looked at didn't use the referencedColumnName but they talked about using it and implied that the owner of the relationship should have the JoinColumn with the mappedBy being used on the other side. I wanted the entity that did not contain the foreign key to be the owner of the relationship so I tried to use the referencedColumnName. I was worried the cascades would only work from the owner side. It turns out the cascades worked on the mappedBy side so I just made the switch and everything works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.