Nice programing

최대 절전 모드 자동 증분 ID

nicepro 2020. 10. 13. 19:21
반응형

최대 절전 모드 자동 증분 ID


주석과 함께 최대 절전 모드를 사용하는 j2ee 응용 프로그램이 있습니다. 내 pojo 클래스의 Id 필드에 주석을 달아 자동 증가 또는 자동 생성으로 설정하는 방법은 무엇입니까? 빈을 추가 할 때 해당 필드를 빈 빈에 두어야합니까?


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

지속 할 때는 그대로 둡니다 null( 0). ( / 래퍼 null를 사용하는 경우 )IntegerLong

어떤 경우에는 AUTO전략에 해결 SEQUENCE보다 라텐 IDENTITY또는 TABLE당신이 수동으로 설정 할 수 있도록, IDENTITY또는 TABLE(기본 데이터베이스에 따라 다름).

시퀀스 이름을 지정하는 것이 SEQUENCE+ 효과가 있는 것 같습니다 .


다음과 같이하십시오.

@Id
@GenericGenerator(name="kaugen" , strategy="increment")
@GeneratedValue(generator="kaugen")
@Column(name="proj_id")
  public Integer getId() {
    return id;
 }

kaugen 대신 임의의 이름을 사용할 수 있습니다. 잘 작동했습니다. 콘솔에서 아래 쿼리를 볼 수 있습니다.

Hibernate: select max(proj_id) from javaproj
Hibernate: insert into javaproj (AUTH_email, AUTH_firstName, AUTH_lastName, projname,         proj_id) values (?, ?, ?, ?, ?)

참고로

사용 넷빈즈 데이터베이스에서 새 엔티티 클래스MySQL은 , * AUTO_INCREMENT * 열 당신에게 다음과 같은 주석 속성을 생성합니다 :

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
@NotNull
private Integer id;

이것은 열이 null이 아니어야한다는 오류와 동일한 오류가 발생했기 때문에 속성을 null로 남겨두고 @NotNull 주석을 제거하면 작동합니다!


Hibernate는 5 가지 유형의 식별자 생성 전략을 정의합니다.

AUTO- 기본 DB에 따라 식별 컬럼, 시퀀스 또는 테이블

TABLE -ID가있는 테이블

IDENTITY -ID 열

SEQUENCE- 시퀀스

신원 복사 – 신원이 다른 엔티티에서 복사됩니다.

테이블 사용 예

@Id
@GeneratedValue(strategy=GenerationType.TABLE , generator="employee_generator")
@TableGenerator(name="employee_generator", 
                table="pk_table", 
                pkColumnName="name", 
                valueColumnName="value",                            
                allocationSize=100) 
@Column(name="employee_id")
private Long employeeId;

자세한 내용은 링크를 확인하십시오 .


자동 증가하려는 숫자 열이있는 경우 columnDefinition직접 설정하는 옵션 일 수 있습니다 . 이는 최대 절전 모드없이 사용 되더라도 스키마가 값을 자동 생성한다는 장점이 있습니다. 이것은 당신의 코드를 db에 한정시킬 수 있습니다 :

import javax.persistence.Column;
@Column(columnDefinition = "serial") // postgresql

PKSerial 유형일 Informix 테이블에 대한 전략을 검색 할 때이 질문에 "범프"가있는 경우 .

나는 이것이 작동한다는 것을 발견했다 ...

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "special_serial_pk")
private Integer special_serial_pk;

이 작업을 수행하려면 session.SaveOrUpdate수행 할 때 special_serial_pk NULL 열의 값을 전달해야합니다 .

제 경우에는 JSON으로 HTML POST수행합니다 .

{
"special_serial_pk": null, //<-- Field to be incremented
"specialcolumn1": 1,
"specialcolumn2": "I love to code",
"specialcolumn3": true
}

Using netbeans New Entity Classes from Database with a mysql auto_increment column, creates you an attribute with the following hibernate.hbm.xml: id is auto increment

참고URL : https://stackoverflow.com/questions/2011528/hibernate-auto-increment-id

반응형