User Tools

Site Tools


hibernate

This is an old revision of the document!


Hibernate is the implementation of the Java Persistence API.

Annotations

Tutorials: http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/

In examples there is:

  • One Department has many employees
  • One Employee has one deparment

ACHTUNG:

  • Bei OneToMany, ManyToOne bezieht sich der erste Teil auf die Klasse, in welcher die annotierte Variable befindet.
    Wenn wir in der Klasse Department das Set employees annotieren - dann steht dort @OneToMany. Weil One Department Many employees hat.
@ManyToOne

Then the modelling will look as following:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
  
    @ManyToOne
    @JoinColumn(name="department_id")
    private Department department;
}
@Entity
@Table(name="DEPARTMENT")
public class Department{
  
    @OneToMany(mappedBy="department")
    private Set<Employee> employees;
}

Discriminators

Discriminators are used for for storing class hierarchies in a single table. If having RedCar.class inheriting from Car.class - both stored in one table, then discriminator tells - hwne t orestore a RedCar and when t orestore a Car.

The element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row.

Details: http://docs.jboss.org/hibernate/core/3.5/reference/en-US/html/mapping.html#mapping-declaration-discriminator

Spring

ManyToOne

The Domain Model looks as following

Campus -is-> Location
CampusPart -is-> Location

Campus -hasmany-> CampusPart

Introduce a Superclass with an ID


@MappedSuperclass
@Table(name = "locations", schema = "public")
public abstract class ELocation {

    @Id
    @Column(name = "ID", unique = true, nullable = false, insertable = false, updatable = false)
    protected UUID id = UuidCreator.getSequential();
}

Introduce an ECampus


@Entity(name="ECampus")
@Table(name = "campuses", schema = "public")
public class ECampus extends ELocation  {

  @OneToMany(mappedBy = "campus", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
  private Set<ECampusPart> campusParts = new HashSet<>();
  
}

Introduce an ECampusPart


@Entity(name = "ECampusPart")
@Table(name = "campusparts", schema = "public")
public class ECampusPart extends ELocation  {


  /*
    Modeling a bidirectional relationship here.

    JoinTable means - that relationships will be extracted explicitely to an own table.
    Allows to explicitely name the columns: campuspart_id and campus_id here

   */
  @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
  @JoinTable(
          name = "campus_campuspart",
          joinColumns = {@JoinColumn(name = "campuspart_id")},
          inverseJoinColumns = {@JoinColumn(name = "campus_id")}
  )
  private ECampus campus;

hibernate.1588152531.txt.gz · Last modified: (external edit)