2013-03-04 1 views
1

Hib는 그 전략이 자리를 잡으면 id 설정을 무시한다.최대 절전 모드 : 사용자 정의 id를 가진 엔티티를 case에 넣는다 : strategy = GenerationType.AUTO

이를 방지하고 전략을 저장하는 방법이 있습니까?

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id") 
public Long getId() { 
    return id; 
} 

...

Account account = new Account(); 
account.setId(1000L); 
account.setSmth(smth); 
... 
//Tried to do so, but tomcat hangs on this string. 
session.createSQLQuery("ALTER TABLE account AUTO_INCREMENT = " + account.getId() + ";").executeUpdate(); 
session.save(account); 
... 

Spasibo!

답변

0

이 전략을 사용하여 필요한 경우 할당 ID로 엔티티를 저장할 수

@GeneratedValue(strategy="org.hibernate.id.Assigned") 
public Long getId() { 
    ... 
} 

유일한 단점은 당신이 Hibernate는이 새로운 또는 기존 엔티티의 경우에 인식 할 필요가있는 버전 필드를 포함해야한다는 것입니다을 :

@Version 
public Long getVersion() { 
    ... 
} 
0

아래와 같이 GeneratedValue의 주석을 제거하면 엔티티는 asssigned id 값을 사용합니다. 그것은 내 webapp에서 작동합니다.

@GeneratedValue(strategy = GenerationType.AUTO) 
0

@GeneratedValue 주석을 제거하십시오. Hibernate는 당신이 지정한 식별자를 사용할 것이다. 아래의 Hibernate 문서에서 오는 설명을 참조하십시오. 할당

응용 프로그램이()를 호출 저장하기 전에 객체 식별자를 할당 할 수 있습니다. 요소가 지정되지 않은 경우 이것이 기본 전략입니다.

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html

참조
관련 문제