Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create child object of existing super object, using JOINED inheritance strategy and Hibernate

Tags:

java

hibernate

what is the appropriate way how create child object of persisted super object with Hibernate?

Consider following example: In database is persisted User with ID 1, firstName Kevin and laseName Smith. By the time is database model extended of new Entity Auditor which is child class of User. For inheritance is used strategy JOINED, so database model now has tow tables: user and auditor. These tables are jointed using user_id FK.

I would like create of Kevin Smith object type Auditor and persist. Problem is that operations are transactional and Hibernate throws NonUniqueObjectException. Exists any way how to safely cast persisted object do child object? I tried to evict given User object, but still the same.

User entity

@Entity
@Table(name = "user")
@Inheritance(strategy = InheritanceType.JOINED)
public class User{

    private Long id;
    private String firstName;
    private String lastName;

    // getters and setters
}

Auditor entity

@Entity
@Table(name = "auditor")
public class Auditor extends User {
  // some properties
}

Logic

public void createAuditorOfUser(final long userId) {
  final User user = userService.getUserById(userId);
  // ...
  final Auditor auditor = new Auditor();
  auditor.setId(user.getId());
  auditor.setFirstName(user.getFirstName());
  auditor.setLastName(user.getLastName());
  userService.evict(user);
  // will throw  NonUniqueObjectException
  auditorService.update(auditor);
  // ...
}

I hope the problem is clear, if not I'll try improve description.

like image 886
Peter Jurkovic Avatar asked Sep 25 '14 12:09

Peter Jurkovic


People also ask

What inheritance mapping strategies are available in hibernate?

Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.

What is @inheritance strategy InheritanceType SINGLE_TABLE?

The annotation @Inheritance is used on the root entity class with strategy = InheritanceType. SINGLE_TABLE . @DiscriminatorColumn is used on the root entity class to specify the discriminator column attributes. Discriminator is a way to differentiate rows belonging to different classes in the hierarchy.

Which inheritance strategy specifies that the superclass and subclass in a hierarchy are mapped to different individual tables?

A “join” strategy, whereby fields or properties that are specific to a subclass are mapped to a different table than the fields or properties that are common to the parent class.

How do you inherit attributes from one class to another?

In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class. superclass (parent) - the class being inherited from.

How to map a parent-child relationship for categories in hibernate?

So in this tutorial, you will learn how to use Hibernate framework to map a parent-child relationship for categories. 1. Design Database Table Create the category table in the database with the following structure: As you can see, the column parent_id is a foreign key that refers to the primary key column category_id of the table itself.

How does inheritance work in hibernate?

Each strategy results in a different database structure. Entity inheritance means that we can use polymorphic queries for retrieving all the subclass entities when querying for a superclass. Since Hibernate is a JPA implementation, it contains all of the above as well as a few Hibernate-specific features related to inheritance.

Which inheritance strategies are supported by JPA and hibernate?

In addition to the shared attributes, the book also stores the number of pages, and the blog post persists its URL. JPA and Hibernate support 4 inheritance strategies which map the domain objects to different table structures. The mapped superclass strategy is the simplest approach to mapping an inheritance structure to database tables.


1 Answers

I think Hibernate intentionally restrict such behaviour. Of course you can do it with native SQL workaround (as you already did).

In Java also we can't cast an object of super class to a sub-class then fill new fields of same object. Instead, create a new instance of sub-class then copy fields.

Seems like one-to-one relation mapping fits better for the functionality you required.

like image 195
hsnkhrmn Avatar answered Oct 25 '22 04:10

hsnkhrmn