Hibernate one-to-one mapping:
Person class:
@Entity
@Table(name="person")
public class Person {
private int personId;
private String name;
private PersonDetails personDetails;
@Id //used for primary key
@GeneratedValue(strategy = GenerationType.IDENTITY) //mysql sequence generation policy
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//cascade type meaning what will happen for associated entity if this entity insert,delete ,update,merge or detach
//defining fetch type meaning if we fetch data then we want to fetch associated entity or not
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "fk_id")
public PersonDetails getPersonDetails() {
return personDetails;
}
public void setPersonDetails(PersonDetails personDetails) {
this.personDetails = personDetails;
}
}
PersonDetails class:
@Entity
public class PersonDetails {
private int personDetailId;
private int income;
private String job;
private String zipCode;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getPersonDetailId() {
return personDetailId;
}
public void setPersonDetailId(int personDetailId) {
this.personDetailId = personDetailId;
}
public int getIncome() {
return income;
}
public void setIncome(int income) {
this.income = income;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
Main class:
public class MainApp {
public static void main(String[] args) {
SessionFactory sessionFactory = (SessionFactory) new Configuration().configure("Hibernate.cfg.xml")
.addAnnotatedClass(Person.class).addAnnotatedClass(PersonDetails.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
try {
Transaction tx = session.beginTransaction();
PersonDetails pdetails = new PersonDetails();
pdetails.setIncome(1000);
pdetails.setJob("engineer");
pdetails.setZipCode("1206");
Person p1 = new Person();
p1.setName("sahabur");
p1.setPersonDetails(pdetails);
session.save(p1);
tx.commit();
}
finally {
session.close();
}
}
}
hibernate.cfg.xml:
<!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>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/person_tracker?useSSL=false&serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">lok094026man</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">5</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment