복붙노트

[SQL] 주석 @Id 및 @GeneratedValue (전략 = GenerationType.IDENTITY)의 사용은 무엇인가? 왜 generationtype 정체성은 무엇입니까?

SQL

주석 @Id 및 @GeneratedValue (전략 = GenerationType.IDENTITY)의 사용은 무엇인가? 왜 generationtype 정체성은 무엇입니까?

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)

왜 우리는이 주석을 사용하고 있습니까? 나는이 내 테이블에 ID 값을 자동 증가하면 알 필요가있다. (GenerationType.IDENTITY)이 우리가이 주석을 사용하는 경우 다른 유형 무슨 일이 실제로 일어나고있다

public class Author extends Domain
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id") 
    private Integer id;

    @Basic(optional = false)
    @Column(name = "name") 
    private String name;

    @Column(name = "address") 
    private String address; 

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "authorId")
    private List<Book>
    bookList;

    public Author()
    { 
        setServiceClassName("wawo.tutorial.service.admin.AuthorService");
    }
}

*이 사용은 무엇입니까? 도메인 추상 클래스를 확장 할 필요가 있습니까?

해결법

  1. ==============================

    1.내가이 질문에 답할 수 있도록 : 우선, 우리의 구성 방법으로 주석을 사용하는 것은 단지 편리한 방법 대신 끝없는 XML 설정 파일을 대처입니다.

    내가이 질문에 답할 수 있도록 : 우선, 우리의 구성 방법으로 주석을 사용하는 것은 단지 편리한 방법 대신 끝없는 XML 설정 파일을 대처입니다.

    현재 엔티티의 기본 키가 아래 @Idannotation는 부재 필드를 나타내는 javax.persistence.Id로부터 상속된다. 따라서 귀하의 최대 절전 모드 및 스프링 프레임 워크는뿐만 아니라 당신이 할 수있는 일부는이 주석에 따라 작품을 반영한다. 자세한 내용은 아이디에 대한 검사의 javadoc를하시기 바랍니다

    @GeneratedValue 주석은 지정된 열 (필드)의 증가의 길을 구성하는 것입니다. MySQL을 사용하는 경우 예를 들어, 당신은 자기-증가하고 사용하기 위해 테이블의 정의에 AUTO_INCREMENT를 지정할 수 있습니다

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    

    표시하기 위해 자바 코드에서 당신은 또한이 데이터베이스 서버 측 전략을 사용하는 것이 인정 것이다. 또한, 당신은 다른 요구 사항에 맞게이 주석의 값을 변경할 수 있습니다.

    예를 들어, 오라클은 증가 방법으로 순서를 사용하는, 우리가 오라클의 시퀀스를 생성 말 :

    create sequence oracle_seq;
    

    이제 우리는 데이터베이스의 순서를 가지고 있지만, 우리는 @SequenceGenerator을 사용하여 자바와 DB 사이의 관계를 확립 할 필요가 :

    @SequenceGenerator(name="seq",sequenceName="oracle_seq")
    

    sequenceName 오라클에서 시퀀스의 실제 이름이고, 이름은 자바에 전화를 할 것입니다. 당신은 그렇지 않으면 단지 이름을 사용, 그것은 이름과 다른 경우 sequenceName를 지정해야합니다. 나는 보통 내 시간을 절약하기 위해 sequenceName을 무시합니다.

    마지막으로, 자바에서이 시퀀스를 사용 할 시간이다. 그냥 @GeneratedValue를 추가 :

    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
    

    발전기 필드는 사용하고자하는 순서 생성을 의미한다. 공지 사항이 DB에 실제 시퀀스 이름이 아닙니다,하지만 이름은 당신이 SequenceGenerator의 이름 필드에 지정된.

    그래서 전체 버전은 다음과 같이해야합니다 :

    public class MyTable
    {
        @Id
        @SequenceGenerator(name="seq",sequenceName="oracle_seq")        
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")               
        private Integer pid;
    }
    

    이제 자바 웹 개발 쉽게하기 위해 이러한 주석을 사용하여 시작합니다.

  2. ==============================

    2.객체 관계형 매핑 맥락에서, 모든 객체는 고유 한 식별자를 가질 필요가있다. 당신은 엔티티의 기본 키를 지정 @Id 주석을 사용합니다.

    객체 관계형 매핑 맥락에서, 모든 객체는 고유 한 식별자를 가질 필요가있다. 당신은 엔티티의 기본 키를 지정 @Id 주석을 사용합니다.

    @GeneratedValue 주석은 기본 키를 생성하는 방법을 지정하는 데 사용됩니다. 귀하의 예제에서는 아이덴티티 전략을 사용하는

    다른 전략이있다, 당신은 더 여기에서 볼 수 있습니다.

  3. ==============================

    3.

    Simply, @Id: This annotation specifies the primary key of the entity. 
    
    @GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used. 
    
    GenerationType enum defines four strategies: 
    1. Generation Type . TABLE, 
    2. Generation Type. SEQUENCE,
    3. Generation Type. IDENTITY   
    4. Generation Type. AUTO
    
    GenerationType.SEQUENCE
    
    With this strategy, underlying persistence provider must use a database sequence to get the next unique primary key for the entities. 
    
    GenerationType.TABLE
    
    With this strategy, underlying persistence provider must use a database table to generate/keep the next unique primary key for the entities. 
    
    GenerationType.IDENTITY
    This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
    
    GenerationType.AUTO
    This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used. 
    

    참조 : - https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html

  4. from https://stackoverflow.com/questions/20603638/what-is-the-use-of-annotations-id-and-generatedvaluestrategy-generationtype by cc-by-sa and MIT license