Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以在没有Hibernate.cfg.xml的情况下配置Hibernate吗_Java_Spring_Hibernate_Spring Mvc - Fatal编程技术网

Java 我们可以在没有Hibernate.cfg.xml的情况下配置Hibernate吗

Java 我们可以在没有Hibernate.cfg.xml的情况下配置Hibernate吗,java,spring,hibernate,spring-mvc,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-con

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

org.hibernate.dialogue.derbydialogue
org.apache.derby.jdbc.ClientDriver
jdbc:derby://localhost:1527/XE
用户名
密码

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

没有必要,在会话工厂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>

org.hibernate.dialogue.derbydialogue
真的


org.hibernate.dialogue.derbydialogue
真的
30
真的
我的包裹

您可以将hibernate.cfg.xml的属性指定为springbean.xml中的属性注入 比如说

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

org.hibernate.dialen.oracle10galent
更新

因此,以类似的方式,您可以将spring中的所有属性指定为sessionFacory的依赖项,您可以通过使用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;
    }
}
公共类TestHibernate{
公共静态void main(字符串arg[]){
Properties prop=新属性();
prop.setProperty(“hibernate.connection.url”,“jdbc:mysql://:/”;
//你可以使用任何你想要的数据库,我已经为Postgres配置了它
prop.setProperty(“方言”,“org.hibernate.dialogue.PostgresSQL”);
prop.setProperty(“hibernate.connection.username”,”);
prop.setProperty(“hibernate.connection.password”和“”);
prop.setProperty(“hibernate.connection.driver_class”,“org.postgresql.driver”);
prop.setProperty(“show_sql”,true);//如果希望查看生成的sql查询
SessionFactory SessionFactory=新配置().addProperties(prop.buildSessionFactory();
Session Session=sessionFactory.openSession();
session.beginTransaction();
Customer user=new Customer();//注意Customer是映射到数据库中Customer表的POJO。
user.setName(“测试”);
user.setisActive(true);
session.save(用户);
session.getTransaction().commit();
session.close();
}
}
@实体
@表(name=“customer”,唯一约束={
@UniqueConstraint(columnNames=“customerid”)}
公共类Customer实现了可序列化{
私有字符串名称;
私人int客户ID;
私有布尔非活动;
公众客户(){
}
公共客户(字符串名称、int customerId、布尔值isActive){
this.name=名称;
this.customerid=customerid;
this.isActive=isActive;
}
/**
*吸气剂
*/
@列(name=“name”,unique=false,nullable=false,length=100)
公共字符串getname(){
返回名称;
}
@身份证
@生成值(策略=标识)
@列(name=“customerid”,unique=true,nullable=false)
public int getcustomerid(){
返回客户ID;
}
@列(name=“isactive”,unique=false,nullable=false)
公共布尔getisactive(){
回报是积极的;
}
/**
*二传手
*/
公共void集合名(字符串名){
this.name=名称;
}
public void setisactive(布尔值isActive){
this.isActive=isActive;
}
}

该文件有什么问题?该文件没有问题,但我想知道是否有其他方法可以配置Hibernate。如果你想知道这没问题,但是,更喜欢使用xml文件。哪一种是最好的。最好和最简洁的答案是我必须使用“Hibernate.dial”而不是“dial”作为设置方言的关键。希望这对别人有帮助!
    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;
    }
}