0% found this document useful (0 votes)
33 views2 pages

Hybernate

This document contains Hibernate configuration properties and code snippets for connecting to a MySQL database, showing SQL queries, updating database tables, adding and fetching data using Hibernate annotations and relationships between entities. Key properties include the MySQL driver, URL, username, password and dialect. It also shows how to save, flush and get data using a session with transactions.

Uploaded by

ANUSHA S G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

Hybernate

This document contains Hibernate configuration properties and code snippets for connecting to a MySQL database, showing SQL queries, updating database tables, adding and fetching data using Hibernate annotations and relationships between entities. Key properties include the MySQL driver, URL, username, password and dialect. It also shows how to save, flush and get data using a session with transactions.

Uploaded by

ANUSHA S G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

<property

name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">an29@06sh</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- To show SQL Query in the console -->


<property name="show_sql">true</property>

<!-- To update the table if it is set to create it will create a new table -->
<property name="hbm2ddl.auto">update</property>

<!-- To commit changes to the table -->


<property name="connection.autocommit">true</property>

<property name="hibernate.allow_update_outside_transaction">true</property>

ADD DATA:
@Entity: To create a entity
@Entity(name= “”): Give specific name to entity
@Table(name=””): To create a table

@Column(name=””): To create a column with specific name

@Transient: To create temporary data, this will not be stored to DB

FETCH DATA:
// define the class which we want to create a table
Configuration config = new
Configuration().configure().addAnnotatedClass(Alien.class);
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
tx.commit();

// save the data to DB


session.save(a);

//to write the data into DB


session.flush();

//to fetch the data we need to use the class name and the
primary key
Alien getV = session.get(Alien.class, 890);

System.out.println(getV);
// close the session
session.close();

We have to override toString() method in entity class

@Embedded: to embed our object into a table


Creating Relationships between two tables:
1. @OneToOne
2. @OneToMany
3. @ManyToOne
4. @ManyToMany

You might also like