【问题标题】:Can we configure Hibernate Without hibernate.cfg.xml我们可以在没有 hibernate.cfg.xml 的情况下配置 Hibernate
【发布时间】:2013-06-24 12:06:51
【问题描述】:

下面是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 应用程序中使用 hibernate.cfg.xml,或者是否有其他配置 Hibernate 的方法。

【问题讨论】:

  • 那个文件有什么问题?
  • 这个文件没有问题,但我想知道是否有任何替代方法来配置 Hibernate。
  • 如果你想知道没关系,但是,最好使用xml文件。哪个是最好的。

标签: java spring hibernate spring-mvc


【解决方案1】:

您可以通过使用 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;
    }
}

【讨论】:

  • 最好最简洁的答案是
  • 我不得不使用“hibernate.dialect”而不是“dialect”作为设置方言的关键。希望这对某人有帮助!
【解决方案2】:

可以在spring bean.xml中指定hibernate.cfg.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 的依赖项

【讨论】:

    【解决方案3】:

    这不是必须的,在会话工厂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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 2020-09-21
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2012-12-20
      • 2021-08-09
      相关资源
      最近更新 更多