views:

38

answers:

1

I can persist an object into the DB from Java as such:

Table Person:

varchar name  
varchar groupID  
varchar emailAddress  
key on (name, groupID)

And in Java

Person foo = new Person("dhackner", "3");  
session.persist(foo);

The two arguments make up the key. In this case, name and groupID are the unique key in the DB, and thus sufficient to distinctly identify any particular row in the table (can have multiple "dhackner" entries, multiple "group 3" entries, but only one "dhackner, group 3" entry).

Assuming correct setup, that code will successfully result in

    name | groupID | emailAddress  
dhackner |       3 |           ""

I would like to be able to do the reverse of that without writing a query or using any autogenerated IDs:

Table Person:

    name | groupID | emailAddress  
dhackner |       3 |  "something"  
dhackner |       4 |     "foobar"  
   other |       3 |     "barfoo"

In Java:

Person foo = new Person("dhackner", "3");  
foo.load(); // something like this
Assert.assertEquals(foo.getEmailAddress(), "something");
+3  A: 

Yes there is, it is called query by example it is pretty straightforward in Hibernate:

Person foo = new Person("dhackner", "3");
foo = session.createCriteria(Person.class).add(Example.create(foo)).uniqueResult();
Assert.assertEquals(foo.getEmailAddress(), "something");
Daff