0

I'm using Eclipse Juno IDE

In phpMyAdmin I created my own database and my Table. Now with the JDBC I entered some recoreds.

Next I tried to implement some queries with the JPA, I created an Entity with the same columns and the persistence.xml connected to my database. but when I was running the program it's delete the all the recoreds and created a new empty table (with the same name) in my database. So my question is : how can i connect to existed table in the database but not create a new one.

another question is: i didn't give to my table a primary key because my table it's about drivers travels, so each driver can travel many times... so if I use the primary key it will not add the new data about the driver. with the JDBC it works fine. but with the JPA the entity needs some field to be a Primary Key.. so how can I add a new data about the same driver in JPA?

the persistence file:

<?xml version="1.0" encoding="UTF-8"?> <persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit transaction-type="RESOURCE_LOCAL" name="MyJPA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>pack.bl.Travels</class> <properties> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/drivers"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="root"/> </properties> </persistence-unit> 

Code:

 EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyJPA"); EntityManager em = emf.createEntityManager(); List<Travels> allTravels = em.createQuery("SELECT t FROM travels t",Travels.class).getResultList(); for (Travels s : allTravels) System.out.println(s.toString()); em.close(); 
7
  • could please show your jpa configuration file Commented Aug 30, 2012 at 6:40
  • i think thre is no problem in your configuration file . Hod did you create your EntityManagerFactory Commented Aug 30, 2012 at 7:05
  • EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyJPA"); Commented Aug 30, 2012 at 7:20
  • would u provide more ur EntityManagerFactory.did u put key-value hibernate.hbm2ddl.auto","create-drop" Commented Aug 30, 2012 at 7:27
  • i added the code. i've noticed it returns nothing so i understood it's create a new table and delete the old one with the data.. Commented Aug 30, 2012 at 7:33

3 Answers 3

1

Add an id autoincrement field to your table. It will be the primary key.

About auto-creation of tables, look here(I assume you are using Hibernate as JPA implementation): Hibernate hbm2ddl.auto possible values and what they do?

Sign up to request clarification or add additional context in comments.

1 Comment

hi, i added an id autoincrement field, but when i'm trying to add new record it gives me a weird exception
1

in your hibernate configuration, make sure hibernate.hbm2ddl.auto isnt set to create-drop (because thats what it sounds like). try setting it to validate http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html

Comments

1

follow the link for jpa configuration

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html

add code in your configuration xml file

<property name="hibernate.hbm2ddl.auto" value="validate"/> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.