permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes How to start JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to start JPA" Watch "How to start JPA" New topic
Author

How to start JPA

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all

I'm new to JPA i 'm using eclipse Galileo and i want to run one JPA program
1) i had created one entity for table
2) i had created one test class to test the entity
3) i added Jpa.jar file in bulid path

But when i build the JPAproject it is showing one error Entity "TableName" has no id or embeded id

please giude me how to over come this i'm not using any hibernate and spring frame work just i'm running in stand alone application


Creativity is nothing but Breaking Rules
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
In your class you have not mentioned the primary key by marking it wihth @Id annotation.

The other one about @EmbeddedId means that you write a different class as the primary key and then you have a reference of it in your class and mark it with @EmbeddedId annotation.

I think if you can share your code we can point at exact location of error.

Hope this helps.


Thanks,
Prabhat
SCJP 1.5, SCWCD 1.5, SCBCD 1.5
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296



Test class




Table name= ActivityMaster
In my table i'm not using any primary key it is just a table to test the JPA program
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

That's a nice, long, piece of code. What would you like to know about it?

Might I suggest starting small and working up. Get something simple going.

I'd suggest using a primary key if you want to save your data. They're not optional in relational databases that use object relational mapping tools like Hibernate.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
Santhosh, you've been around long enough to know that you should be using UseCodeTags. Please edit your post accordingly.


Ping & DNS - my free Android networking tools app
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

They're not optional in relational databases that use object relational mapping tools like Hibernate.


is it not possible to run the JPAGetResultList program with out using hibernate
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
is it not possible to run the JPAGetResultList program with out using hibernate

Well, you must use *some* JPA implementation. If you don't want to use Hibernate, then OpenJPA and EclipseLink are other options. But, thanks to JPA being a standard, they should all work the same.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Have you tried to run it? Regardless of what JPA implementation you use, be it TopLink or Hibernate or EJB, you'll need a primary key and an id field in your JPA entity.

A good way to learn is to run programs and look at the result. Does it run? Does it work? Does it throw a runtime exception? Does it throw a runtime error?

Run the program! Then, if you run into a problem, explain the problem and we'll try and help you understand what is happening.

If you're not sure of the purpose of a primary key in a database table, you might want to start learning a bit more about databases and tables before jumping into JPA and Hibernate programming. The purpose of a primary key or id field in a JPA entity is really the same as the purpose in a database table.
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
Since you dont have primary key in your table then you just cannot do it.

However there is another thread which talks on the similar line.
Hibernate Mapping problem because no primary key in database

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
I'm using openJPA now and i'm getting the following runtime error

javax.persistence.PersistenceException:

The following is my persistance.xml file



Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I also tried the same but with primaryKey as seqno.and i also get the same error !


Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa

what to do now !

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
can any body tell how to run openJPA for stand alone application
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
when i run the program it giving me



Error:javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 1.1.2.v20090612-r4475): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null]. Verify that you have set the expected driver class and URL. Check your login, persistence.xml or sessions.xml resource. The jdbc.driver property should be set to a class that is compatible with your database platform




Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
In your mapping i see this



and in your code i see this



Change it to

It should work.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
I had changed to JPADemo
but still im getting the runtime error when i execute the first statement

EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPADemo", System.getProperties());


org.eclipse.persistence.exceptions.PersistenceUnitLoadingException


Please giude me how to run openjpa in j2se application i was struck from last two days
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
Your system.getProperties() is overriding the properties that you have set.

Just use the plain one createEntityManagerFactory("JPADemo");
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
now the exception is changed as

javax.persistence.PersistenceException: No Persistence provider for EntityManager named examplejpa



and in my test code im using
Prabhat Jha
Ranch Hand

Joined: Aug 13, 2007
Posts: 58
Do you have necessary jar files in your class file ?

Also where is your persistence.xml located it should be in META-INF.

I think it is taking quite some time to fix your problem so i will say share your project structure and all the details like which all jars you have placed and where?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to start JPA