首页 > 解决方案 > How can I change DiscriminatorValue of entity

问题描述

I would ask you about opportunity to change DiscriminatorValue for some entyty. For example I have 2 entity:

    @Entity
    @Table(name = "user")
    @Inheritance(strategy=InheritanceType.JOINED)
    @DiscriminatorColumn(name = "user_type")
    @DiscriminatorValue("CUSTOMER")
    public class User

and

    @Entity
    @Table(name = "businessUser")
    @DiscriminatorValue("FOUNDER")
    public class BusinessUser extends User

I have repos:

    @NoRepositoryBean
    public interface UserBaseRepository<T extends User>  extends JpaRepository<User, Long> {

    T findByEmail(String email);
    }

    @Transactional
    @Repository("UserRepository")
    public interface UserRepository extends UserBaseRepository<User> {
    }

    @Transactional
    @Repository("BusinessUserRepository")
    public interface BusinessUserRepository extends UserBaseRepository<BusinessUser> {
    }

At first I created simple user. This entity has user_type CUSTOMER. Then I would like to expand that entity and modify it to BusinessCustomer. When I tried to do it the following: find user entity and to cast to businessUser entity I get IllegalCastException. I cannot to cast User to BusinessUser.

Help me please, How I can change user_type or or update user to businessUser.

Have a nice day, Thanks

标签: hibernatespring-bootjpaspring-data-jpaentity

解决方案


为了做这样的事情,我认为你不得不使用原生查询。实际上,我确实认为在所描述的情况下使用继承并不是最好的选择,但那是另一回事了。


推荐阅读