7

以下是hibernate.cfg.xml

<?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>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/XE</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
</session-factory>
</hibernate-configuration>

我想知道是否总是需要hibernate.cfg.xml在每个 Hibernate 应用程序中使用,或者是否有任何替代方法来配置 Hibernate。

4

3 回答 3

12

您可以通过使用 java 设置属性来做到这一点

    public class TestHibernate {

        public static void main(String arg[]) {
        Properties prop= new Properties();

        prop.setProperty("hibernate.connection.url", "jdbc:mysql://<your-host>:<your-port>/<your-dbname>");

        //You can use any database you want, I had it configured for Postgres
        prop.setProperty("dialect", "org.hibernate.dialect.PostgresSQL");

        prop.setProperty("hibernate.connection.username", "<your-user>");
        prop.setProperty("hibernate.connection.password", "<your-password>");
        prop.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
        prop.setProperty("show_sql", true); //If you wish to see the generated sql query

    SessionFactory sessionFactory = new Configuration().addProperties(prop).buildSessionFactory();
       Session session = sessionFactory.openSession();
    session.beginTransaction();
            Customer user = new Customer(); //Note customer is a POJO maps to the customer table in the database.

    user.setName("test");
    user.setisActive(true);
    session.save(user);
    session.getTransaction().commit();
    session.close(); 

    }

    }


@Entity
@Table(name = "customer", uniqueConstraints = {
        @UniqueConstraint(columnNames = "customerid")})
public class Customer implements Serializable{

    private String name;
    private int customerid;
    private boolean isActive;

    public Customer() {
    }

    public Customer(String name, int customerId, boolean isActive) {
        this.name = name;
        this.customerid = customerId;
        this.isActive = isActive;
    }

    /**
     *      GETTERS 
     */

    @Column(name = "name", unique = false, nullable = false, length = 100)
    public String getname() {
        return name;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "customerid", unique = true, nullable = false)
    public int getcustomerid() {
        return customerid;
    }

    @Column(name = "isactive", unique = false, nullable = false)
    public boolean getisactive() {
        return isActive;
    }


    /**
     *      SETTERS 
     */
    public void setname(String name) {
        this.name = name;
    }

    public void setisactive(boolean isActive) {
        this.isActive = isActive;
    }
}
于 2017-06-28T17:47:34.000 回答
4

没有必要,在会话工厂 bean 配置中,您可以使用直接传递这些值

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
            <prop key="hibernate.show_sql"></prop>
            <prop key="hibernate.use_outer_join">true</prop>
        </props>
    </property>

前任

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
            <prop key="hibernate.show_sql"></prop>
            <prop key="hibernate.use_outer_join">true</prop>
            <prop key="hibernate.jdbc.batch_size" >30</prop>
            <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>mypackage</value>
        </list>
    </property>
</bean>
于 2013-06-27T06:01:05.180 回答
0

例如,您可以将 hibernate.cfg.xml 的属性指定为 spring bean.xml 中的属性注入

<property name="hibernateProperties">  
    <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>

因此,您可以以类似的方式将 spring 中的所有属性指定为 sessionFacory 的依赖项

于 2013-06-27T06:34:48.860 回答