0

I am doing a migration of old project (Core Java + EJB + hibernate) from hibernate 3 to hibernate 5.2.12. Though hibernate 5.2.12 supports .hbm.xml file but as a part up gradation migrating from .hbm files to Annotated files.

Below is the scenario i have, and the data model is tightly coupled classes.

public interface BaseEntity extends Serializable, Cloneable, Observable
{
    public void setId(long id);

    public long getId();

    ... few other generic attributes
}

public class BaseClass implements BaseEntity {

    protected long id;

    public void setId(long id)
    {
        this.id = id;
    }

    public long getId()
    {
        return objectId;
    }
.. few other attributes
}


public class AuditableBaseClass extends BaseClass implements BaseEntity, Audit
{
    ... few other attributes
}


public class Car extends AuditableBaseClass {
    .... some attributes
}


public class Bike extends AuditableBaseClass {
    ...some attributes
}

public class Truck extends AuditableBaseClass {
    ... some attributes
}

If we look at the existing .hbm.xml file below, Used different sequence for the different classes

<class name="Car" table="Car">      
    <id name="id" type="long" column="id">
      <generator class="sequence">
        <param name="sequence">SEQ_CAR</param>      
      </generator>
    </id>
    <
    // some other properties
</class>

<class name="Bike" table="Bike">
    <id name="id" type="long" column="id">
      <generator class="sequence">
        <param name="sequence">SEQ_BIKE</param>         
      </generator>
    </id>
    // some other properties
</class>


<class name="Truck" table="Truck">
    <id name="objectId" type="long" column="id">
      <generator class="sequence">
        <param name="sequence">SEQ_TRUCK</param>        
      </generator>
    </id>
</class>

Simple definition for @Id using sequence is as below

@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "sequence-generator" )
@SequenceGenerator( name = "sequence-generator", sequenceName = "SEQ_NAME" )

For a fact that i know we need to use @MappedSuperclass to represent the super class. How to represent sequence attribute for an @Id column which is using a different sequence for different child classes though all classes inherit the attribute id but uses different sequences for each.

Any help on this please?

user1876040
  • 431
  • 1
  • 7
  • 18
  • Possible duplicate of [Specifying distinct sequence per table in Hibernate on subclasses](https://stackoverflow.com/questions/4560813/specifying-distinct-sequence-per-table-in-hibernate-on-subclasses) – Bahadir Tasdemir Mar 20 '18 at 07:46

2 Answers2

2

At first you must set @MappedSuperclass for BaseClass and use @GeneratedValue for attribute id with fix generator. After that use @SequenceGenerator in child class and set name attribute with generator in BaseClass.

@MappedSuperclass
public class BaseClass implements BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq_gen")
    @Column(name = "ID")
    protected long id;

    public void setId(long id)
    {
        this.id = id;
    }

    public long getId()
    {
        return objectId;
    }
.. few other attributes
}

public class AuditableBaseClass extends BaseClass implements BaseEntity, Audit
{
    ... few other attributes
}

@Entity
@Table(name="car")
@SequenceGenerator(sequenceName = "car_id_seq", allocationSize = 1, name = "my_seq_gen")
public class Car extends AuditableBaseClass {
    .... some attributes
}

@Entity
@Table(name="bike")
@SequenceGenerator(sequenceName = "bike_id_seq", allocationSize = 1, name = "my_seq_gen")
public class Bike extends AuditableBaseClass {
    .... some attributes
}

@Entity
@Table(name="truck")
@SequenceGenerator(sequenceName = "truck_id_seq", allocationSize = 1, name = "my_seq_gen")
public class Truck extends AuditableBaseClass {
    ... some attributes
}
Ardeshir Ayati
  • 113
  • 1
  • 5
  • i have the same issue but in our system, each table has different sequence generator and generator value in @GeneratedValue is not constant. what to do in this situation ? – Mahdiyar Zerehpoush Jun 19 '20 at 16:00
0

Hibernate 5.2 doesn't allow us to have two identifier generators with the same name even they have different configurations because the scope is global as stated in JPA Spec. Because of that, you need to define your identifiers and generators in the entity classes.

A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

Reference: https://hibernate.atlassian.net/browse/HHH-12329 by @Vlad Mihalcea