EntityManager不将实体保存到MySQL数据库表

时间:2013-12-19 13:49:17

标签: java

我使用JPA 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");

当我执行时没有错误它只是没有将它保存到表中(城市表存在且架构是相同的

3 个答案:

答案 0 :(得分: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 :(得分:1)

我会尝试显式刷新em.persist(),em.flush(),em.close()。只是为了验证。

答案 2 :(得分:0)

您应该创建一个Default no-args构造函数

public City() {    }

Hibernate使用反射来创建对象