Приложение не завершает работу после вставки данных с использованием JPA

У меня есть требование, где я должен создать таблицу и вставить данные в MySQL, используя Hibernate.

Я могу создать данные и вставить их в таблицу, но моя проблема в том, что основной метод не завершается после выполнения. Сервисный метод продолжает выполняться после печати "=======DONE======="

Пожалуйста, дайте мне знать, как я могу решить эту проблему

Версия MySql Версия Hibernate Упорство xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.inc.Greeting</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/maersk"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto"  value="update"/>
</properties>
</persistence-unit>
</persistence>

Сервисный класс

public class Test {
     EntityManager em = null;
    void initEntityManager() {
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPU");
         em = emf.createEntityManager();
    }
    void create() {
         em.getTransaction().begin();
         Greeting g_en = new Greeting("hel", "en");
         Greeting[] greetings = new Greeting[]{g_en};
         for(Greeting g : greetings) {
             em.persist(g);
         }
         em.getTransaction().commit();
         System.out.println();
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.initEntityManager();
        test.create();
        System.out.println("================== DONE ===================");

    }
}

Класс сущности

@Entity
public class Greeting implements Serializable {
    @Id
    @GeneratedValue
    private int id;
    @Column(name="message",length=10)
    private String message;
    @Basic
    private String language;

    public Greeting() {
    }

    public Greeting(String message, String language) {
        this.message = message;
        this.language = language;
    }
}

Логи в консоли

Jan 29, 2015 3:33:46 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jan 29, 2015 3:33:46 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jan 29, 2015 3:33:46 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Jan 29, 2015 3:33:46 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: MyPU
    ...]
Jan 29, 2015 3:33:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Jan 29, 2015 3:33:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 29, 2015 3:33:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 29, 2015 3:33:46 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jan 29, 2015 3:33:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jan 29, 2015 3:33:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/maersk]
Jan 29, 2015 3:33:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Jan 29, 2015 3:33:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jan 29, 2015 3:33:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jan 29, 2015 3:33:47 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Jan 29, 2015 3:33:47 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: maersk.greeting
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [message, id, language]
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
Jan 29, 2015 3:33:48 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete

========================================================

1 ответ

Решение

Попробуйте закрыть EntityManagerFactory пример.

Другие вопросы по тегам