My linux world » Java – Hibernate

Java - Hibernate


Contents

Dependencies

If you use maven, add this to your pom.xml:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>${hibernate.version}</version>
</dependency>

JPA (Java Persistence API)

Create entity

// begin transaction:
session.beginTransaction();
 
// save entity :
session.save(entity);
 
// then commit :
session.getTransaction().commit();

Read entity

Object entity = session.get(MyImplementation.class,"MyImplementationID");

Update entity

// begin transaction: 
entityManager.getTransaction().begin();
 
// we use "merge" method from entityManager :
entityManager.merge(entity);
 
// then commit :
entityManager.getTransaction().commit();

Delete entity

// begin transaction: 
session.beginTransaction();
 
// delete entity :
session.delete (entity);
 
// then commit :
session.getTransaction().commit();

Entity relationship

One to One

@Entity
@Table(name = "EntityA ")
public class EntityA {
 
@Id
@Column(name="EntityA_ID") 
private String id;
 
@OneToOne
@PrimaryKeyJoinColumn
private EntityB entityB;
 
}
@Entity
@Table(name = "EntityB ")
public class EntityB {
 
@Id
@Column(name="EntityB_ID") 
private String id;
 
@OneToOne(mappedBy="entityB", cascade = CascadeType.ALL)
private EntityA entityA;
 
}

Many to One

@Entity
@Table(name = "EntityA")
public class EntityA {
 
@Id
@Column(name="EntityA_ID") 
private String id;
 
@OneToMany(mappedBy = "entityB", cascade = CascadeType.ALL)
private List<EntityB> entityBs;
 
}
@Entity
@Table(name = "EntityB")
public class EntityB {
 
@Id
@Column(name="EntityB_ID") 
private String id;
 
@ManyToOne(optional = false)
@JoinColumn(name = "EntityA_ID")
private EntityA entityA;
 
}

Many to Many

@Entity
@Table(name = "EntityA")
public class EntityA {
 
@Id
@Column(name="EntityA_ID") 
private String id;
 
@ManyToMany(cascade=CascadeType.ALL, mappedBy="entityAs")
private Set<EntityB> entityBs;
 
}
@Entity
@Table(name = "EntityB")
public class EntityB {
 
@Id
@Column(name="EntityB_ID") 
private String id;
 
@ManyToMany(cascade=CascadeType.ALL)  
@JoinTable(name="entityA_entityB", joinColumns=@JoinColumn(name="EntityB_ID"), inverseJoinColumns=@JoinColumn(name="EntityA_ID"))
private Set<EntityA> entityAs;
 
}

Schema

Create Schema

To get created schema scripts :

SchemaExport schemaExport = new SchemaExport();	
 
schemaExport
.setOutputFile( outputFile.getAbsolutePath() )
.create( EnumSet.of(TargetType.SCRIPT) , metadataImplementor);

To execute created schema scripts :

SchemaExport schemaExport = new SchemaExport();
schemaExport.create( EnumSet.of(TargetType.DATABASE) , metadataImplementor);
List<?> exceptions = schemaExport.getExceptions();

Update Schema

To get updated schema scripts :

SchemaUpdate schemaUpdate = new SchemaUpdate();
 
schemaUpdate
.setOutputFile(outputFile.getAbsolutePath())
.execute(EnumSet.of(TargetType.SCRIPT) , metadataImplementor);

To execute updated schema scripts :

SchemaUpdate schemaUpdate = new SchemaUpdate();
schemaUpdate.execute(EnumSet.of(TargetType.DATABASE) , metadataImplementor);
List<?> exceptions = schemaUpdate.getExceptions();

Drop Schema

To get droped schema scripts :

SchemaExport schemaExport = new SchemaExport();	
 
schemaExport
.setOutputFile( outputFile.getAbsolutePath() )
.drop( EnumSet.of(TargetType.SCRIPT) , metadataImplementor );

To execute droped schema scripts :

SchemaExport schemaExport = new SchemaExport();
schemaExport.drop(EnumSet.of(TargetType.DATABASE) , metadataImplementor);
List<?> exceptions = schemaExport.getExceptions();

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.