相关疑难解决方法(0)

在Hibernate 4中创建会话工厂

我在Hibernate 4中生成会话工厂时遇到了麻烦.在Hibernate 3中我简单地做了:

org.hibernate.cfg.Configuration conf= HibernateUtil
    .getLimsInitializedConfiguration(systemConfiguration
.getHibernateconfFile());

SessionFactory sf = conf.configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

现在我需要将ServiceRegistry类传递给buildSessionFactory,但是Javadocs对于如何解决这个问题非常模糊.有小费吗?

java hibernate

62
推荐指数
5
解决办法
15万
查看次数

Hibernate 5: - org.hibernate.MappingException:未知实体

org.hibernate.MappingException: Unknown entity当我尝试将hibernate 5.0与mysql集成时,我收到错误消息

这似乎是hibernate5.0.0和5.0.1的问题.这适用于hibernate 4.3.9

Maven依赖

<dependency>
    <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

的hibernate.cfg.xml

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3307/SampleDB
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</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>

    <mapping class="UserA.User"></mapping>

</session-factory>
Run Code Online (Sandbox Code Playgroud)

HibernateMain.java代码

package UserA;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate hibernate-5.x

14
推荐指数
3
解决办法
2万
查看次数

Hibernate 4注释配置

我正在尝试使用仅带注释的Hibernate 4和一个hibernate.cfg.xml文件.我已经制作了自己的注释,并使用反射将其添加到配置中.我能够以这种方式使用Hibernate 4,但我的配置是使用不推荐的方法构建的.

final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
    configuration.addAnnotatedClass(clazz);
}
return configuration.configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

(不推荐的代码:) buildSessionFactory();.

即使是hibernate 4文档也显示以这种方式构建配置.

如果我尝试使用新方法(buildSessionFactory(ServiceRegistry)我没有得到相同的结果,似乎有很多不必要的代码完全按照不推荐使用的方法做的那样.但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码.

我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4?我似乎只是造成错误并面临不必要的困难.

java hibernate hibernate.cfg.xml hibernate-annotations hibernate-4.x

7
推荐指数
1
解决办法
6276
查看次数

寻找一个不被弃用的会话工厂

我正在处理hibernate,当我打开我当前的项目时,我发现我的Session-Factory已被弃用:

AnnotationConfiguration af = new AnnotationConfiguration();
SessionFactory factory = af.configure().buildSessionFactory();
Session session = factory.openSession();
Run Code Online (Sandbox Code Playgroud)

AnnotationConfiguration现在似乎已经被弃用了......所以我检查了JavaDoc并告诉它它被移动到:

org.hibernate.cfg.Configuration

我的代码到目前为止工作得很好,实际上我不想改变它......但是我用Google搜索并发现有人问自己同样的问题为什么SessionFactory需要更改... http://rgordon.co.uk/博客/ 2012/02/24 /休眠-请-不要-贬低自己动手/

该文章是从2012年开始的(所以不是那么老......)并以这种方式描述所有内容:

ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(properties);
ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();

Configuration configuration = new Configuration().addClass(FeedTradePersistable.class);

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)

我实现了那个.JavaDoc再次证明了错误!已过时.它指的是:

org.hibernate.boot.registry.StandardServiceRegistryBuilder

我再次用Google搜索.结果不那么令人满意......

我开始修改代码......

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Run Code Online (Sandbox Code Playgroud)

抛出异常......

org.hibernate.HibernateException:未设置'hibernate.dialect'时,Connection不能为null

在线:

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)

我很确定这是因为我没有指定任何配置设置.实际上,我不想.我对hibernate.cfg.xml感到满意.

我玩了一下配置.addFile(.. - 不是那么成功......

有没有人对此有所了解?谢谢

更新:(hibernate.cfg.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC …
Run Code Online (Sandbox Code Playgroud)

java hibernate sessionfactory hibernate-annotations sql-server-2012-express

5
推荐指数
2
解决办法
1万
查看次数

Hibernate 实体类映射不起作用

由于某种原因,除非我使用方法,否则我的类映射不起作用addAnnotatedClass(CategoryEntity.class)。我已经花了很多时间,但仍然无法弄清楚这里的问题是什么,你能告诉我我做错了什么吗?

休眠配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">admin</property>

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/dvdrental</property>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="entity.CategoryEntity"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

类别Entity.java

import javax.persistence.*;
import java.sql.Timestamp;

@Table(name = "category", schema = "public", catalog = "dvdrental")
@Entity
public class CategoryEntity {
    private Integer categoryId;
    private String name;
    private Timestamp lastUpdate;

    @Id
    @Column(name = "category_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getCategoryId() {
        return categoryId;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java hibernate

3
推荐指数
1
解决办法
4818
查看次数