Spring Entity继承,在使用findOne时从存储库获取null

时间:2017-03-16 20:23:20

标签: java spring-mvc jpa inheritance spring-data-jpa

在春天,JPA,我想将我的用户模型扩展到更多用户类型。 我喜欢它在一张桌子里。

我相信我的榜样是正确的,但有些我如何获得null方法的findOne

你能帮助我吗,我无法弄清楚出了什么问题

这是我的代码(我有更多用户类型,但基本不起作用):

用户:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_type")
public abstract class User {
    public LicenseType license = LicenseType.FREE;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long id;

    public String username;
    public String password;

    @OneToOne(fetch = FetchType.LAZY)
    public Country country;

    @OneToOne(fetch = FetchType.LAZY)
    public UserPhoto profilePhoto;
    .... 
    ....
}

UserBasic

@Entity
@DiscriminatorValue("UserBasic")
public class UserBasic extends User {

}

UserRepository

@NoRepositoryBean
public interface UserRepository<T extends User> extends Repository<T, Long>{
    T findOne(Long id);
    T findByFacebookId(Long facebookId);
}

UserBasicRepository

public interface UserBasicRepository extends UserRepository<UserBasic>{
    @Modifying
    @Query(value="UPDATE User SET someField = now() WHERE id = ?1", nativeQuery= true)
    long updateSomeField(Long userId);
}

UserService

public class UserServiceImpl implements UserService {

    @Autowired
    private UserBasicRepository userBasicRepository;

    @Override
    public User findOne(Long userId) {

        /**
         *
         *  THIS WILL RETURN NULL
         *  vv
         *
         */

        return userBasicRepository.findOne(userId);
    }
    ....
    ....
}

问题更新

现在评论后,我打印了查询,我想我看到了问题

and userbasic0_.user_type='UserBasic'

我只是想继承,没有要求额外的评论。我怎样才能消除这种情况。

Hibernate: 
select
    userbasic0_.id as id2_8_0_,
    userbasic0_.password as passwor12_8_0_,
    userbasic0_.username as usernam15_8_0_,
    .....
from
    User userbasic0_ 
where
    userbasic0_.id=? 
    and userbasic0_.user_type='UserBasic'

0 个答案:

没有答案