【问题标题】:Needed to restart Eclipse to do Hibernate changes需要重新启动 Eclipse 才能进行 Hibernate 更改
【发布时间】:2016-10-25 20:23:14
【问题描述】:

我正在学习 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>

我该如何解决这个问题?

【问题讨论】:

    标签: java eclipse hibernate


    【解决方案1】:

    你需要关闭会话工厂

    factory.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多