-->
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: help on targetEntity
PostPosted: Tue Dec 22, 2009 12:16 am 
Newbie

Joined: Mon Dec 14, 2009 5:38 am
Posts: 13
Hello,
I have two classes like these:

Code:
@Entity
@Table(name="banks")
public class Banks {
    private Long id;
    private List contact_persons;
    ....
    @OneToMany(targetEntity=BankContactPerson.class)
    @IndexColumn(name=idx)
    public List getContact_persons() {
    ....
    }
}


and the child:

Code:
@Entity
@Table(name="bank_contact_persons")
public class BankContactPerson() {
    private Long id;
    private String name;
    ....
}


if I want to populate bank (with the child also), it will generate an error like this:

Code:
WARNING: SQL Error: 1146, SQLState: 42S02
SEVERE: Table 'db.banks_bank_contact_persons' doesn't exist
SEVERE: >>org.hibernate.exception.SQLGrammarException: could not initialize a collection: [willow.beansdao.Bank.contact_persons#5]


I never declare table "banks_bank_contact_persons". it seems that it comes from table "banks" specified in Bank class, and table "bank_contact_persons" in BankContactPerson class.

and if I change the table name in BankContactPerson class to be "contact_persons" and the table name in database to be "banks_contact_persons" (hoping that the resulting table will be "banks_contact_person"), the error is like this:

Code:
SEVERE: Table 'indoscale-e.contact_persons' doesn't exist


how can I solve this? the child of "banks" table is "bank_contact_persons"

thank you


Top
 Profile  
 
 Post subject: Re: help on targetEntity
PostPosted: Tue Dec 22, 2009 4:12 am 
Beginner
Beginner

Joined: Tue Nov 03, 2009 9:38 am
Posts: 24
The problem is that the 'one' side of the relation, i.e. the Bank entity, takes ownership of the relation.
Thus, you need to declare a join column or join table, depending on whether your bank_contact_persons table should contain the bank's id as a foreign key or not.

You did declare nothing and thus Hibernate will asume you have a join table by default and name it <entity A tablename>_<entity B tablename>, i.e. in your case banks_bank_contact_persons.

You now have two options:
1. Add a @JoinColumn to your List and give it the name of the column in bank_contact_persons that contains the bank id.
2. Add a @JoinTable annotation and provide your own join table.


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.