Skip to content

Instantly share code, notes, and snippets.

@MostafijurJ
Last active October 15, 2020 09:56
Show Gist options
  • Save MostafijurJ/b69011ca2ef6b154806f6434f159f24c to your computer and use it in GitHub Desktop.
Save MostafijurJ/b69011ca2ef6b154806f6434f159f24c to your computer and use it in GitHub Desktop.
Connection procedure of hibernate.
Here is the following steps for connecting hibernate into mysql database.
Step-1: After Creating a maven project first we need to add this two dependency into the pom.xml file.
`<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>`
Step-2: Create a model class with some informations and create constructor and getter(),setter() method.
Step-3: Create a object of the model class using and by using setter() method set some values of these variables.
step-4: Then it needs to create hibernate.cfg.xml file into the resources directory. After creating this here need to add some configurations properties.
**
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">Kajol</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
**
Step-5: Then it needs to configure in the main class.
Configuration con = new Configuration();
con = con .configure().addAnnotatedClass(Informations.class);
con = con.configure().addResource("hibernate.cfg.xml");
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
session.save(info);
Transaction tx = session.beginTransaction();
tx.commit();
@MostafijurJ
Copy link
Author

this is comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment