package org.hibernate.test.annotations.uniqueconstraint;
import org.junit.Test;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.fail;
/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
public class UniqueConstraintTest extends BaseCoreFunctionalTestCase {
protected Class[] getAnnotatedClasses() {
return new Class[]{
Room.class,
Building.class,
House.class
};
}
@Test
public void testUniquenessConstraintWithSuperclassProperty() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Room livingRoom = new Room();
livingRoom.setId(1l);
livingRoom.setName("livingRoom");
s.persist(livingRoom);
s.flush();
House house = new House();
house.setId(1l);
house.setCost(100);
house.setHeight(1000l);
house.setRoom(livingRoom);
s.persist(house);
s.flush();
House house2 = new House();
house2.setId(2l);
house2.setCost(100);
house2.setHeight(1001l);
house2.setRoom(livingRoom);
s.persist(house2);
try {
s.flush();
fail("Database constraint non-existant");
} catch(JDBCException e) {
//success
}
tx.rollback();
s.close();
}
}