2013-12-19 31 views
1

我使用JPA MySQL來執行查詢數據庫,但是當我嘗試仍然存在一些實體沒有行被添加到表的EntityManager不保存實體MySQL數據庫表

這裏是persistense.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
      version="2.0"> 

    <persistence-unit name="avtoparki" transaction-type="RESOURCE_LOCAL"> 
     <description> 
      Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide 
     </description> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>Entities.City</class> 

     <properties> 
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/world" /> 
      <property name="javax.persistence.jdbc.user" value="root" /> 
      <property name="javax.persistence.jdbc.password" value="tauren993" /> 

      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
      <property name="hibernate.show_sql" value="true" /> 
      <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> --> 
     </properties> 

    </persistence-unit> 

</persistence> 

這裏是實體類:

@Entity 
public class City { 
    @Id 
    //@GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name="ID") 
    int id; 

    @Column(name="Name") 
    String name; 

    @Column(name="CountryCode") 
    String CountryCode; 

    @Column(name="District") 
    String District; 

    @Column(name="Population") 
    int Population; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getCountryCode() { 
     return CountryCode; 
    } 
    public void setCountryCode(String countryCode) { 
     CountryCode = countryCode; 
    } 
    public String getDistrict() { 
     return District; 
    } 
    public void setDistrict(String district) { 
     District = district; 
    } 
    public int getPopulation() { 
     return Population; 
    } 
    public void setPopulation(int population) { 
     Population = population; 
    } 

這裏是代碼:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("avtoparki"); 


EntityManager em = emf.createEntityManager(); 

City city = new City(); 
city.setCountryCode("Georgia"); 
city.setDistrict("AVOIE"); 
city.setName("Tbilisi"); 
city.setId(1); 
em.persist(city); 
em.close(); 
System.out.println("SAVED"); 

當我執行沒有錯誤,它只是不將其保存到表(城市表存在和架構是相同的

+0

你有沒有打過電話'他們。在em.close()之前使用flush()'? – micha

+0

看起來像手動實體管理器創建,所以我希望手動啓動和提交事務。 – Gimby

回答

2

對我來說,你錯過了一筆交易。嘗試保存城市的對象與下面的代碼:

private void save (City city, EntityManagerFactory emf) { 
    EntityManager  em = emf.createEntityManager(); 
    EntityTransaction tx = em.getTransaction(); 

    try { 
     tx.begin(); 
     em.persist(city); 
     tx.commit(); 
    } catch(RuntimeException ex) { 
     if(tx != null && tx.isActive()) tx.rollback(); 
     throw ex; 
    } finally { 
     em.close(); 
    } 
} 
1

我想嘗試一個明確的沖洗em.persist(),em.flush( ),em.close()。只是爲了驗證。

0

您應該創建一個默認的無參數的構造

public City() { } 

Hibernate使用反射來創建對象

相關問題