HHH-5947: Test case

This commit is contained in:
Manuel Bernhardt 2011-02-22 15:49:49 +01:00 committed by Emmanuel Bernard
parent 753b95729a
commit 928354827a
4 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package org.hibernate.test.annotations.uniqueconstraint;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
@MappedSuperclass
public class Building {
public Long height;
private Room room;
public Long getHeight() {
return height;
}
public void setHeight(Long height) {
this.height = height;
}
@ManyToOne
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
}

View File

@ -0,0 +1,35 @@
package org.hibernate.test.annotations.uniqueconstraint;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(name = "uniqueWithInherited", columnNames = {"room", "cost"} )})
public class House extends Building {
public Long id;
public Integer cost;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getCost() {
return cost;
}
public void setCost(Integer cost) {
this.cost = cost;
}
}

View File

@ -0,0 +1,32 @@
package org.hibernate.test.annotations.uniqueconstraint;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
@Entity
public class Room {
private Long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -0,0 +1,52 @@
package org.hibernate.test.annotations.uniqueconstraint;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.TestCase;
/**
* @author Manuel Bernhardt <bernhardt.manuel@gmail.com>
*/
public class UniqueConstraintTest extends TestCase {
protected Class[] getAnnotatedClasses() {
return new Class[]{
Room.class,
Building.class,
House.class
};
}
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();
}
}