Discussions

EJB programming & troubleshooting: Creating My First EJB3.0 Entity Bean with Eclipse and JPA

  1. Hi I am creating my first entity bean using EJB3.0, JBoss 4.0 GA, OracleXE and JPA and Eclipse 3.3 and I haven't been able to get to the point where I can get automatic Intellisense listing of the database tables and column names within my Eclipse IDE when I do something like @Table (name=XXX) or @Column(name=XXX). I'm also having problems with my @Entity annotation - the error message says: Unable to find persistence unit = XXX. My problem seems to start when Eclipse opens the JPA Facet dialog box especially where it asks me to select a JPA implementation or user library but I have not created any of such things yet. One additional question: is it better to use annotations for entity beans or just plain old orm.xml. I feel that with annotations, you may have to change code and re-compile but with orm.xml, you only need to change the file settngs but the JPA specs prefer annotations. Pls advise. Thank you in advance.
  2. Getting you started[ Go to top ]

    I feel your pain re finding persistence unit. I have worked through it a bit and have managed to get something working. As you correctly stated, you need to provide the JPA implementation. Your obvious choices are Hibernate or Toplink. But where to set them up? It seems the Eclipse GUI tools for this are a letdown. This has worked for me with Hibernate: 1) Define the "provider" in persistence.xml. For Hibernate it is "org.hibernate.ejb.HibernatePersistence". See the sample below. <!--?xml version="1.0" encoding="UTF-8"?--> org.hibernate.ejb.HibernatePersistence com.foo.Foo 2) Then it is simply a matter of getting the Hibernate libraries to support this. This is what I did: a) Grab hibernate-3.2.6.ga and import all the jars to build path b) Grab hibernate-entitymanager-3.3.1.GA and do the same c) Make sure your test code looks for JPAtest and it should work. This link helped me, especially section 2.2: http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/ Interestingly, the MyEclipse JPA plugin does all this out of the box for you very nicely. Up and running in 2 minutes. I may try out the Toplink implementation next. Let me know how you go! If anyone has any more detail they can add here, that would be great. Cheers, Pat
  3. Thank you Pat for the input. I was reading another article on this the other day and interestingly, the author had created the table in the database and then used Eclipse (inversely) to create the entity bean from the table. However, I understand we can generate the entity bean coding in Java first and then Eclipse will auto-generate the database schema. Can it be done both ways?
  4. Yes.. you can go either way. Creating entities from schema is called reverse engineering. This is the way I usually work as I still think in terms of databases. However the trend is of course to think of objects and not to worry about how the persistence to database works. I dont trust this approach yet.
  5. I have done application by using Glassfish server you can try with JBoss4.2 but websphere6.1 does not support EJB3.0 for it you have to download feature pack. Follow this link you will able to run the application. http://www.myeclipseide.com/documentation/quickstarts/jpa/index.html#Resources