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();
eliorsh
  • 195
  • 1
  • 5
  • 20

3 Answers3

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?

Community
  • 1
  • 1
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • hi, i added an id autoincrement field, but when i'm trying to add new record it gives me a weird exception – eliorsh Aug 30 '12 at 13:51
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

radai
  • 23,949
  • 10
  • 71
  • 115
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"/>
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37