需要重启Eclipse以执行Hibernate更改

时间:2016-06-23 13:11:11

标签: java eclipse hibernate

我正在学习Hibernates并且在练习时遇到了这个奇怪的问题,有时,当我进行更改并运行程序时,我的eclipse控制台突然停止显示最后一行Hibernate: drop table UserDetails。要使程序运行,我需要重新启动Eclipse程序。

以下是我的代码。

UserDetails.java

package org.hibernates.dto;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class UserDetails {
    @Id
    @Column(name = "User_ID")
    private int userId;
    @Column(name = "User_Name")
    private String userName;
    @Temporal(TemporalType.TIME)
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDesription() {
        return desription;
    }

    public void setDesription(String desription) {
        this.desription = desription;
    }

    @Lob
    private String address;
    private String desription;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

HibernatesTest.java

package org.hibernates.dto;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernatesTest {

    public static void main(String[] args) {

        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("User 1");
        user.setDate(new Date());
        user.setAddress("User Address");
        user.setDesription("User Desription");
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = (StandardServiceRegistryBuilder) new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = builder.build();
        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);

        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();

        session.save(user);
        tx.commit();
        session.close();
    }

}

hibernate.cfg.xml中

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://U0138039-TPD-A\\SQLEXPRESS:1433;DatabaseName=HibernatesDataBase</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">T!ger123</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="org.hibernates.dto.UserDetails" />

    </session-factory>

</hibernate-configuration>

请让我知道我哪里出错了,我该如何解决这个问题。

由于

1 个答案:

答案 0 :(得分:1)

您需要关闭会话工厂

factory.close()
相关问题