Hibernate get Object by non ID , unique identifier

2019-07-20 04:20发布

I have the following object:

    @Id
    @GeneratedValue
    private long id;
    @Column(name = "uniqueId", unique=true)
    private String uniqueId;

is it possible to get an object from the DB that has object.uniqueId == "some_unique_id"??

thanks.

1条回答
姐就是有狂的资本
2楼-- · 2019-07-20 04:59
String hql = "select foo from Foo foo where foo.uniqueId = :uniqueId";
return (Foo) session.createQuery(hql)
                    .setString("uniqueId", theUniqueId)
                    .uniqueResult();
查看更多
登录 后发表回答