-->
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: JPA and generics
PostPosted: Mon Aug 29, 2011 2:06 pm 
Newbie

Joined: Mon Aug 29, 2011 12:22 pm
Posts: 5
Hello,

I'm trying to implement a system which is defined in an API, but JPA entities won't be found by hibernate. I'm using a JBoss AS6 server by the way.

The api layer describes domain subjects and actions to take over them. For example an applicant will submit an application for processing and expect and answer with messages out of this processing.
Code:
public interface Application {

    Applicant getApplicant();
    void setApplicant(Applicant applicant);
}


And the implementation is as follows
Code:
@Entity
public class ApplicationJPA implements Application {

    private ApplicantJPA applicant;

    public Applicant getApplicant() { return this.applicant; }
    public void setApplicant(Applicant applicant) { this.applicant = applicant; }
}

@Entity
public class ApplicantJPA implements Applicant {
}


But this code causes hibernate to complain it's expecting an ApplicantJPA and not an Applicant with the following exception
Code:
DEPLOYMENTS IN ERROR:
  Deployment "persistence.unit:unitName=test.war#testPU" is in error due to the following reason(s): org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ApplicationJPA.applicant references an unknown entity: Applicant
  Deployment "<UNKNOWN jboss.j2ee:jar=test.war,name=ApplicationJPADAO,service=EJB3>" is in error due to the following reason(s): ** UNRESOLVED Demands 'persistence.unit:unitName=test.war#testPU' **


So, I've change them into generics. The Application interface now looks like
Code:
public interface Application<T extends Applicant> {

    T getApplicant();
    void setApplicant(T applicant);
}


This is causing some problems with collections and with lengthly declarations since here I'm only using one attribute, Applicant, but as you may guess there are many more. So, I changed to an in-line approach
Code:
public interface Application {

    <T extends Applicant> T getApplicant();
    <T extends Applicant> void setApplicant(T applicant);
}


But this causes me confusion as to how to declare the attribute in the JPA implementation
Code:
@Entity
public class ApplicationJPA implements Application {

    private ApplicantJPA applicant; // This causes the set method to fail

    public Applicant getApplicant() { return this.applicant; }
    public void setApplicant(Applicant applicant) { this.applicant = applicant; }
}


In your experience what would be a better approach and/or how to correct the technique.

Thanks in advance


Top
 Profile  
 
 Post subject: Re: JPA and generics
PostPosted: Wed Aug 31, 2011 9:57 am 
Newbie

Joined: Mon Aug 29, 2011 12:22 pm
Posts: 5
At the end the targetEntity attribute in the relations annotations saved the day and I didn't need generics at all.


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.