• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to start JPA

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh, you've been around long enough to know that you should be using UseCodeTags. Please edit your post accordingly.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body tell how to run openJPA for stand alone application
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your mapping i see this



and in your code i see this



Change it to

It should work.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your system.getProperties() is overriding the properties that you have set.

Just use the plain one createEntityManagerFactory("JPADemo");
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic