-->
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.  [ 5 posts ] 
Author Message
 Post subject: JPA inheritance issue
PostPosted: Mon Aug 10, 2009 8:32 am 
Newbie

Joined: Fri Aug 07, 2009 9:21 am
Posts: 4
Hello!

First of all excuse me for my english, it's not very good.

I have a super class named say A with the @MappedSuperclass annotation, and i have a class named say B which extends A.

In class A i have defined a field named profession, which returns a Person, it's as follows:

Code:
@MappedSuperclass
public class A {

@JoinColumn(name="profession")
@ManyToOne(optional=false)
private Person profession;

public Person getProfession() {
return profession;
}

}


In class B, which extends A, i need to overwrite field profession so it returns a Doctor instead a Person, as follows:

Code:
public class B extends A {
@JoinColumn(name="profession")
@ManyToOne(optional=false)
private Doctor profession;

public Doctor getProfession() {
return profession;
}

}


As you can imagine, Doctor extends Person.

So far so good, the problem appears when i run my application, i get a message in my Tomcat logs saying:

Quote:
Caused by: org.hibernate.MappingException: Duplicate property mapping of profession found in org.admon.db.bienes.modelo.DesincorporacionInmuebles


It is like i can not overwrite a field defined in the super class, because it says i have a duplicate property.

Am i doing something wrong?

I hope i was clear, and again, sorry if my english is not that good.

Thanks in advance for your time.
Regards!


Top
 Profile  
 
 Post subject: Re: JPA inheritance issue
PostPosted: Tue Aug 11, 2009 8:18 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, you really are mapping the same name twice in the subclass, right? So, there is a duplicate. If the property in the sublcass had a different name, you'd be fine.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: JPA inheritance issue
PostPosted: Tue Aug 11, 2009 12:36 pm 
Newbie

Joined: Fri Aug 07, 2009 9:21 am
Posts: 4
Hello Mr. Cameron, thanks for your answer.

Well, i changed the property name in the subclass and now there is no duplicate, but now i have another error:

Quote:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.tests.class column: profession (should be mapped with insert="false" update="false")


I understand why i'm getting that error, but now i'm thinking that maybe i should not declare that field profession in the super class, is there something i can do to solve this? some advice about what i should do?

Thanks for your time, and thanks again Mr. Cameron.


Top
 Profile  
 
 Post subject: Re: JPA inheritance issue
PostPosted: Tue Aug 11, 2009 1:21 pm 
Newbie

Joined: Fri Aug 07, 2009 9:21 am
Posts: 4
Hello again, i answer to myself.

I've changed my super class, i did not put the @JoinColumn(name="profession") to declare the property in the super class, as follows

i changed this:

Code:
@MappedSuperclass
public class A {

@JoinColumn(name="profession")
@ManyToOne(optional=false)
private Person profession;

public Person getProfession() {
return profession;
}

}


for this:

Code:
@MappedSuperclass
public class A {

@ManyToOne(optional=false)
private Person profession;

public Person getProfession() {
return profession;
}

}


Now i do not get a Repeated column in mapping for entity error.
I hope it's correct what i did, if not, i would thank you if you let me know.

Regards...


Top
 Profile  
 
 Post subject: Re: JPA inheritance issue
PostPosted: Wed Aug 12, 2009 2:03 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
not sure what that would create at database level, you'd have to try.

An option I'd try would be to generify the thing and make the getPerson method return a generic type.

So you'd get something like
Code:
public abstract class A<T extends Person> {
    @ManyToOne
    protected Person profession;

   public abstract T getProfession();
}

////////

public class B extends A<Doctor> {
    @SuppressWarnings("unchecked")
    public Doctor getProfession() {
       return (Doctor) profession;
    }
}



I haven't tried this, don't know if it'd work, but it's what I'd try first.


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