Urbanleg
Urbanleg

Reputation: 6542

Hibernate get Object by non ID , unique identifier

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.

Upvotes: 2

Views: 4199

Answers (1)

JB Nizet
JB Nizet

Reputation: 692003

String hql = "select foo from Foo foo where foo.uniqueId = :uniqueId";
return (Foo) session.createQuery(hql)
                    .setString("uniqueId", theUniqueId)
                    .uniqueResult();

Upvotes: 4

Related Questions