0
좋은 하루! 고유 ID 및 고유 NAME을 사용하여 엔티티를 만들려고합니다. 그래서, 여기 Hibernate 고유 한 annotation이 작동하지 않는다
@MappedSuperclass
public class BaseEntityImpl implements Serializable, BaseEntity
{
@Id
@GeneratedValue
protected Long id;
@Column(unique = true)
protected String name;
//here are getters and setters
}
@Entity
public class SimpleEntity extends BaseEntityImpl
{
}
여기 내 DAO
@Transactional(propagation = Propagation.MANDATORY)
public class SimpleDaoImpl extends HibernateDaoSupport implements SimpleDao
{
public SimpleEntity createOrUpdate(SimpleEntity entity)
{
getSession().saveOrUpdate(entity);
return entity;
}
}
입니다 그리고 내가이 테스트가 예외를 던질 것으로 예상
@Test
public void create()
{
SimpleEntity entity = new SimpleEntity();
entity.setName("name1");
SimpleEntity entity2 = new SimpleEntity();
entity2.setName("name1");
simpleDao.createOrUpdate(entity);
simpleDao.createOrUpdate(entity2);
}
(메모리 더비 DB에 사용) 내 단위 테스트입니다 왜냐하면 나는 같은 이름의 엔티티 두 개를 저장하려고 시도하지만 괜찮습니다.
어디서 잘못 되었나요?
데이터베이스를 만들 때 속성을 설정 했습니까? –
죄송합니다, 무슨 속성이 있습니까? –
'unique' 속성은 스키마 생성에만 사용되며 삽입/업데이트에 고유성을 적용하지 않습니다. 당신이 최대 절전 모드로 당신의 스키마를 생성하게한다면, 당신은 예외를 야기 할 데이터베이스 레벨에서'name'에 고유 한 제약 조건을 가져야 만합니다. 참조 : http://stackoverflow.com/questions/4105164/hibernate-unique-column-constraint-being-ignored – DannyMo