Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we configure Hibernate Without hibernate.cfg.xml

The below is the 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>

I wonder if it's always necessary to use hibernate.cfg.xml in every Hibernate Application or there is any alternative way to configure Hibernate.

like image 710
Mounica Reddy Avatar asked Jun 27 '13 05:06

Mounica Reddy


People also ask

How to configure hibernate without XML and using XML files?

Hibernate Configuration Without XML And Using XML Files. In this article we are going to configure hibernate using java class i.e. without XML file and using XML file. We can configure hibernate using java class so no need to add hibernate.cfg.xml and can provide mapping of entities via annotated entity classes so no need to provide mapping files.

Is it mandatory to use Hibernate config file?

No, it's not mandatory to use hibernate.cfg.xml. Just don't use .configure (). Hibernate will look for hibernate.cfg.xml if we use .configure ().

What is the use of configuration class in hibernate?

Hibernate facilitates to provide the configurations either in an XML file (like hibernate.cfg.xml) or properties file (like hibernate.properties). An instance of Configuration class allows specifying properties and mappings to applications.

Why is hibernate not finding a specific file in a folder?

I believe it is because of your configure () call on the Configuration object. Try removing that and hibernate will not look for the non-existent file.


2 Answers

You can do this by setting the properties using 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;
    }
}
like image 142
Ajinkya Karande Avatar answered Oct 14 '22 12:10

Ajinkya Karande


It is not necessary, in the session factory bean configuration you can pass these values directly using

    <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>

ex

<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>
like image 27
Arun P Johny Avatar answered Oct 14 '22 10:10

Arun P Johny