How to configure Hibernate when you are using a Spring Session Factory

我是研究僧i 提交于 2019-12-04 16:05:58

Hibernate Tools is not available under Hibernate 4.x version. It is available with version 3.2. If you are using maven, the dependency goes as:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools</artifactId>
    <version>3.2.4.GA</version>
    <scope>runtime</scope>
</dependency>

Now, the hibernate configuration for the tools got nothing to do with spring. An example xml will be (values in this example is for sql server):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="reversengineeringfactory">
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://myinstance:port/mydb</property>
        <property name="hibernate.connection.username">dbuser</property>
        <property name="hibernate.connection.password">dbpass</property>
        <property name="hibernate.default_catalog">mydb</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    </session-factory>
</hibernate-configuration>

Now, For configuring the hibernate config xml in eclipse, you should choose Hibernate Perspective --> Edit Configuration --> Go for configuration File Setup.

A sample reverse engineering xml is below (gives granular control over code generation)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
    <table-filter match-schema="dbo" match-name="Employee"
        package="com.maggu.domain.model" />
    <table-filter match-schema="dbo" match-name="Company"
        package="com.maggu.domain.model" />

    <table schema="dbo" name="Employee">
        <primary-key>
            <generator class="identity" />
        </primary-key>
    </table>
    <table schema="dbo" name="Company">
        <primary-key>
            <generator class="identity" />
        </primary-key>
    </table>
</hibernate-reverse-engineering>

HTH

if you are using annotations to create your persistence entities than you can do like:

<bean id = "mySessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">


        <property name = "packagesToScan">
        <list>
            <value>entityclasses</value>
        </list>
        </property>

where entityclasses is the package which contains all your entity classes. Let me know if it helps!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!