-->
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.  [ 4 posts ] 
Author Message
 Post subject: Wrong column mapping using @ManyToOne?
PostPosted: Fri Aug 29, 2008 7:57 am 
Newbie

Joined: Fri Aug 29, 2008 7:28 am
Posts: 3
I'm using JBoss 4.0.5 GA w/ EJB3 (bundled Hibernate version 3.2.0.ga), MySQL 5.0.51b on WinXP x64, Java 1.6.0_07 64-bit.

I have two entities:

Code:
package com.airgonomix.entities;

import javax.persistence.*;
import java.util.Date;
import java.util.Collection;
import java.io.Serializable;

@Entity
@Table(name="holidays")
public class Holiday implements Serializable {

    private int holidayId;
    private Date holidayStart;
    private Date holidayEnd;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="hol_int_holiday_id")
    public int getHolidayId() {
        return holidayId;
    }

    public void setHolidayId(int holidayId) {
        this.holidayId = holidayId;
    }

    @Column(name="hol_dt_holiday_start")
    @Temporal(TemporalType.DATE)
    public Date getHolidayStart() {
        return holidayStart;
    }

    public void setHolidayStart(Date holidayStart) {
        this.holidayStart = holidayStart;
    }

    @Column(name="hol_dt_holiday_end")
    @Temporal(TemporalType.DATE)
    public Date getHolidayEnd() {
        return holidayEnd;
    }

    public void setHolidayEnd(Date holidayEnd) {
        this.holidayEnd = holidayEnd;
    }
}


and

Code:
package com.airgonomix.entities;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="holidays_by_tenant")
@IdClass(HolidayByTenantKey.class)
public class HolidayByTenant implements Serializable {
    private int holidayId;
    private int tenantId;
    private Tenant tenant;
    private Holiday holiday;

    @Id
    @Column(name="hol_int_holiday_id")
    public int getHolidayId() {
        return holidayId;
    }

    public void setHolidayId(int holidayId) {
        this.holidayId = holidayId;
    }

    @Id
    @Column(name="tnt_int_tenant_id")
    public int getTenantId() {
        return tenantId;
    }

    public void setTenantId(int tenantId) {
        this.tenantId = tenantId;
    }

    @ManyToOne
    @PrimaryKeyJoinColumn(name="tnt_int_tenant_id", referencedColumnName="tnt_int_tenant_id")
    public Tenant getTenant() {
        return tenant;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    @ManyToOne
    @PrimaryKeyJoinColumn(name="hol_int_holiday_id", referencedColumnName = "hol_int_holiday_id")
    public Holiday getHoliday() {
        return holiday;
    }

    public void setHoliday(Holiday holiday) {
        this.holiday = holiday;
    }
}


The last one references another entity, Tenant, but that's not important right now (and I'm not using ManyToMany mapping between Tenant and Holiday, since HolidayByTenant will contain other attributes eventually). All tables and columns are explicitly defined in the above code, but I can add a screenshot of the database structure, if it'll help.

Now, I'm trying to execute a very simple query - to fetch all Holidays for a given tenantId:

Code:
select hbt.holiday from HolidayByTenant hbt where hbt.tenantId = 1


Hibernate generates the following SQL:

Code:
select
      holiday1_.hol_int_holiday_id as hol1_39_,
      holiday1_.hol_dt_holiday_start as hol2_39_,
      holiday1_.hol_dt_holiday_end as hol3_39_
from
     holidays_by_tenant holidaybyt0_
  inner join
     holidays holiday1_
         on holidaybyt0_.holiday_hol_int_holiday_id=holiday1_.hol_int_holiday_id
  where
     holidaybyt0_.tnt_int_tenant_id=1


Look on what column it's trying to join - holiday_hol_int_holiday_id]. Since I don't have such a column in holidays_by_tenant table in my database, the query fails:

Code:
...
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
   at com.airgonomix.ejb.utils.UtilsBean.joinTest(UtilsBean.java:224)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'holidaybyt0_.holiday_hol_int_holiday_id' in 'on clause'


What could be the reason for Hibernate to create this field mapping? Any help will be greatly appreciated. I have debug-level logs from Hibernate, but hadn't noticed anything relevant there.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 7:44 am 
Newbie

Joined: Thu Aug 07, 2008 4:19 am
Posts: 4
I am not quite sure, but instead of

@PrimaryKeyJoinColumn(name="hol_int_holiday_id", referencedColumnName = "hol_int_holiday_id")

try this

@JoinColumn(name = "hol_int_holiday_id")


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 2:45 pm 
Newbie

Joined: Fri Aug 29, 2008 7:28 am
Posts: 3
Thanks for the suggestion, I'd done exactly that (obviously had to also disable updates and inserts for that mapping), and it works. However, the question still stands why would Hibernate mangle the column name? Weird...


Top
 Profile  
 
 Post subject: Re: Wrong column mapping using @ManyToOne?
PostPosted: Thu Jan 07, 2010 10:45 am 
Newbie

Joined: Thu Jan 07, 2010 10:42 am
Posts: 1
Have you been able to solve this?

It looks like the name property of @PrimaryKeyJoinColumn is not taken into account for a @ManyToOne mapping


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.