-->
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: Problem with using interfaces and annotations
PostPosted: Thu Apr 14, 2005 2:25 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0 final with annotations 3.0 beta 1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I have an interface class that I use to implement several concrete entities. For exaple:
Code:
public interface AgreementRole
{
    public Agreement getAgreement();
}

@Entity
public class EmploymentAgreementRole implements AgreementRole
{
    ....
    private EmploymentAgreement agreement;

   

    @ManyToOne
    @JoinColumn(name = "emp_agreement_id", referencedColumnName = "emp_agreement_id")
    public EmploymentAgreement getAgreement()
    {
        return agreement;
    }
}

As you can see from above, the implementation method getAgreement returns a concrete implementation of the Agreement interface but hibernate keeps asking for the interface:

Code:
org.hibernate.MappingException: Could not determine type for: com.xxx.Agreement, for columns: [org.hibernate.mapping.Column(agreement)]


How do I successfully use Abstract classes or Interface because it seems like hibernate keeps referring to them instead of the concrete entity classes.? I'm not using the inheritence annotations since I don't really need a main table for the parent class/interface.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 5:52 pm 
Newbie

Joined: Thu Apr 07, 2005 6:09 pm
Posts: 3
I'm currently investigating the same issue and the understanding I have at this point is the @ManyToOne (and if required the @OneToMany) has an optional attribute called targetEntity which is a fully qualified name of the implementation class.

I belive if you augment the implementation class with

@ManyToOne (targetEntity="com.xxx.EmploymentAgreement")

this will then allow Hibernate to correctly identify that the mapped class is not the interface but is rather the implementation class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 5:53 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
I found from another thread http://forum.hibernate.org/viewtopic.php?t=940942&highlight=targetentity that the targetEntity might be the way to go when using interfaces or subclasses. So I did the following:

Code:

// original interface
public interface AgreementRole
{
    public Agreement getAgreement();
}


// modified implementation class
public class EmploymentAgreementRole implements AgreementRole
{
    ....
    private EmploymentAgreement agreement;

    @ManyToOne((targetEntity = "com.xxx.EmploymentAgreement")
    @JoinColumn(name = "emp_agreement_id", referencedColumnName = "emp_agreement_id")
    public EmploymentAgreement getAgreement()
    {
        return agreement;
    }
}


But it's still giving the same error asking for the Agreement interface so I'm at a loss.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 6:01 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
Looks like we posted at the same time, jlowcock. Another thing I tried was to convert the interface into an abstract class like this:
Code:
public abstract class AgreementRole
{
    @Transient
    public abstract Agreement getAgreement();
}


public class EmploymentAgreementRole extends AgreementRole
{
    ....
    private EmploymentAgreement agreement;

    @ManyToOne((targetEntity = "com.xxx.EmploymentAgreement")
    @JoinColumn(name = "emp_agreement_id", referencedColumnName = "emp_agreement_id")
    public EmploymentAgreement getAgreement()
    {
        return agreement;
    }
}


The implementation/overriding of the method is fine but I get a different error saying that the agreement property is being defined twice even though I said Transient in the abstract class:
Code:
org.hibernate.MappingException: duplicate property mapping: agreement
   at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:327)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:317)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:186)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:815)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 6:31 pm 
Beginner
Beginner

Joined: Mon Mar 28, 2005 12:58 pm
Posts: 27
I finally found the problem and it seems to be the fact that the implementation method getAgreement() was returning the concrete class instead of the interface:

Code:
@ManyToOne((targetEntity = "com.xxx.EmploymentAgreement")
@JoinColumn(name = "emp_agreement_id", referencedColumnName = "emp_agreement_id")
    public EmploymentAgreement getAgreement()
    {
        return agreement;
    }


So when I changed it to the following it worked:
Code:
@ManyToOne((targetEntity = "com.xxx.EmploymentAgreement")
@JoinColumn(name = "emp_agreement_id", referencedColumnName = "emp_agreement_id")
    public Agreement getAgreement()
    {
        return agreement;
    }



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.