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.
Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy. table per subclass. table per concrete class.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With