java - 我们可以配置 Hibernate 没有 hibernate.cfg.xml

标签 java spring hibernate spring-mvc

下面是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。

最佳答案

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

关于java - 我们可以配置 Hibernate 没有 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335530/

相关文章:

java - 如何检查 Java Spring 数据库查询成功/失败?

database - 我的 H2/C3PO/Hibernate 设置似乎没有保留准备好的语句?

java - 扩展抽象构造函数?

java - 在 JMapViewer 中绘制折线

java - 字符串引用?

java - 封闭表达式,以 n 表示

java - EnableAutoConfiguration Spring 注解是如何工作的?

spring - 使用注释摆脱 *-portlet.xml。可能的?如何?

java - 如何在运行时在 Tomcat 中创建一个 hibernate.properties 文件

java - 编写生成发票程序的标准方法是什么?