NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

jpa controller classes from entity classes

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
PierreC
Posted via mailing list.





PostPosted: Tue Apr 10, 2012 4:40 am    Post subject: jpa controller classes from entity classes Reply with quote

Hello,

When you use the "jpa controller classes from entity classes" wizard, you
get a controller class which constructor looks like this :

public MembresJpaController(UserTransaction utx, EntityManagerFactory
emf) {
this.utx = utx;
this.emf = emf;
}

My question is simple : can you give an example on how should this class be
instanciated and, in particular, how to create the two parameters that needs
to be passed?

How can does the app read the information about the persistence unit
contained in the persistance.xml file?

I'm using Glassfish 3.1 and Netbeans 7.1 and my project doesn't use JSF.
Just plain servlets/JSP with JPA to manage persistance.

Thanks in advance for the help.

--
View this message in context: http://netbeans-org.1045718.n5.nabble.com/jpa-controller-classes-from-entity-classes-tp5628942p5628942.html
Sent from the Netbeans - J2EE mailing list archive at Nabble.com.
Back to top
leomendoza123



Joined: 10 Jul 2012
Posts: 1

PostPosted: Tue Jul 10, 2012 8:44 pm    Post subject: Reply with quote

BUMP Embarassed

i have the same question
Back to top
sitongia



Joined: 20 Aug 2008
Posts: 29

PostPosted: Thu Sep 27, 2012 5:57 pm    Post subject: Reply with quote

Bump +1

In a project I built before using this approach, the JpaController got a no-args constructor that instantiated the EntityManagerFactory, as in
Code:

   public FileJpaController() {
      emf = Persistence.createEntityManagerFactory("ImageDBPU");
   }

Today, I get
Code:

    public Tbl1996JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }

which is a 1-arg constructor, and differs from the OP's 2-arg constructor!

What is determining how this code is code-generated from this wizard?

What happened to the no-args constructor?

Thanks,
==Leonard
Back to top
sitongia



Joined: 20 Aug 2008
Posts: 29

PostPosted: Thu Sep 27, 2012 6:00 pm    Post subject: Re: jpa controller classes from entity classes Reply with quote

On 09/27/2012 11:57 AM, sitongia wrote:
Quote:
Bump +1

Hmmm... I can see now that when I used the forum, it sent my mail
without any context, which would have been there if I'd replied to the
email instead of using the forum.

Here is the forum entry:
http://forums.netbeans.org/viewtopic.php?t=47442&highlight=jpa+controller+constructor

--
==Leonard E. Sitongia
High Altitude Observatory
National Center for Atmospheric Research
P.O. Box 3000 Boulder CO 80307 USA
address-removed voice: (303)497-2454 fax: (303)497-1589
Back to top
Sergey.Petrov@Oracle.Com
Posted via mailing list.





PostPosted: Thu Sep 27, 2012 8:18 pm    Post subject: Re: jpa controller classes from entity classes Reply with quote

It was changed a few releases ago. Current behavior is to generate
controller with entity manager/factory as external argument
Sergey

27.09.2012 21:57, sitongia
Back to top
asmith2306



Joined: 07 Jan 2012
Posts: 6

PostPosted: Fri Nov 02, 2012 12:17 pm    Post subject: Reply with quote

I have no idea if the OP has figured this one out as its an old post but I will provide the answer here to people who are still stuck with this. I recently read in a Netbeans EE book that uses these JPA controllers is "that once you generate the controllers, all you have to do to persist objects to a db is to invoke methods on the controllers"... and they left it there, didn't explain what to pass to the constructor of the generated controller classes or how to use them!! So here goes:

- generate your entity classes from your database
- generate your jpa controller classes from the entity classes
- Inject an EntityManagerFactory object and a UserTransaction object into whatever class where you want to persist the object.

I am using a simple JSF managed bean that takes input from a web form, creates a Person entity object, sets the objects name and age fields with the values received from the form, persists the entity object and then forwards on to another web page.
Code:

@ManagedBean
@RequestScoped
public class PersonBean {

    private String name;
    private String age;
   
    @PersistenceUnit(unitName="JSFPU") //inject from your application server
    EntityManagerFactory emf;
    @Resource //inject from your application server
    UserTransaction utx;
   
    public PersonBean() {
       
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
   
    public String action(){ //this methods name corresponds to the commandbuttons action attribute on the web form
        PersonEntity pe = new PersonEntity(); //create an instance of your entity class
        pe.setAge(this.age);
        pe.setName(this.name);
       
        PersonEntityJpaController pejc = new PersonEntityJpaController(utx, emf); //create an instance of your jpa controller and pass in the injected emf and utx
        try {
            pejc.create(pe); //persist the entity
        } catch (RollbackFailureException ex) {
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "result"; //forward to an xhtml page called result.xhtml
    }
}


If you are confused as to how the emf and utx objects work without being instantiated... the javadoc for the @Resource annotation states "When the annotation is applied to a field or method, the container will inject an instance of the requested resource into the application component when the component is initialized" (the container being your application server.). Same goes for the @PersistenceUnit annotation which specifies the name of your persistence.xml file which needs to be created in order for it to work. If you don't have one use the Netbeans persistence wizard to create one. Here, the application server sends an instance of both the EntityManagerFactory and UserTransaction for you to work with. Hope that helps, Alan.

ps. it makes no difference that I am using JSF. Just inject the EMF and UTX into any class where you need to persist an entity and it will work the same from there.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo