복붙노트

[SPRING] @Component Hibernate 클래스

SPRING

@Component Hibernate 클래스

내 프로그램에서 주석 달린 수업을 최대 절전 모드로 전환했습니다. Spring 프로젝트를 실행하고 있기 때문에 servlet.xml 파일 (com.student.dto는 실제 패키지 이름 임)에 이들을 포함시키고 @Component를 Contacts Entity에 추가했습니다. @Component를 자동화하는 방법이 있습니다. 모든 절전 클래스에서. 모델을 만들 때마다,이 작업을 끝내고 이렇게하는 것이 더 나을 것이라고 생각합니다.

<context:component-scan base-package="com.student.dto" />




@Component
@Entity
@Table(name = "Contacts", catalog = "javadb")
public class ContactsDTO implements java.io.Serializable {

    private int idContacts;
    private StudentDTO StudentDTO;
    private String addr1;
    private String addr2;
    private String city;
    private String state;
    private String pinCode;
    private String country;
    private String phone;
    private String mobile;
    private String email;
    private String contactscol;

    public ContactsDTO() {
    }

    public ContactsDTO(int idContacts) {
        this.idContacts = idContacts;
    }

    public ContactsDTO(int idContacts, StudentDTO StudentDTO, String addr1,
            String addr2, String city, String state, String pinCode,
            String country, String phone, String mobile, String email,
            String contactscol) {
        this.idContacts = idContacts;
        this.StudentDTO = StudentDTO;
        this.addr1 = addr1;
        this.addr2 = addr2;
        this.city = city;
        this.state = state;
        this.pinCode = pinCode;
        this.country = country;
        this.phone = phone;
        this.mobile = mobile;
        this.email = email;
        this.contactscol = contactscol;
    }


   getters & setters

해결법

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

    1.너는 모든 일을 잘못하고있다. 스프링 빈은 기본적으로 싱글 톤이며 엔티티는 스레드로부터 안전하지 않으며 절대 존재하지도 않습니다.

    너는 모든 일을 잘못하고있다. 스프링 빈은 기본적으로 싱글 톤이며 엔티티는 스레드로부터 안전하지 않으며 절대 존재하지도 않습니다.

    엔티티는 Persistent Context에 바인드 된 로컬 변수 여야합니다. 다중 스레드 환경에서 액세스 할 수 없습니다.

    동시성 제어는 데이터베이스에 의해 처리되며 응용 프로그램 논리는 응용 프로그램 수준의 반복 가능한 읽기를 통해 손실 된 업데이트를 방지하는 데 주로 관심을 가져야합니다.

    DAO 및 서비스는 스프링 싱글 톤 구성 요소 여야합니다. 엔티티와 바인딩 된 DTO를 요구하면 결코 싱글 톤이되어서는 안됩니다. 이러한 객체는 수명이 짧고 객체를 생성 한 요청에 적용됩니다.

    Spring Data JPA 문서에서 견고한 데이터 액세스 레이어 디자인을 확인하십시오.

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

    2.@Component javadocs는 이것을 말한다.

    @Component javadocs는 이것을 말한다.

    @Component, @Repository 등은 일반적으로 자동 검사 (응용 프로그램 부트 스트랩 중) 및 종속성 주입에 사용됩니다. 엔티티를 Spring 구성 요소로 만들 때 중요한 부분이 없습니다. 엔터티의 일반적인 사용은 관계형 데이터베이스 테이블을 나타내는 것입니다. 엔티티 (Java) = 테이블 (RDBMS). 엔티티의 정의는 다음과 같습니다.

    엔티티를 포함하는 방법은 다음과 같아야합니다.

        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                <property name="dataSource" ref="dataSource"/>
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                        <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
                    </props>
                </property>
    
                <property name="annotatedClasses">
        <list>
    <!-- Here are your Entities annotated with @Entity -->
        </list>
        </bean>
    

    "annotatedPackages"라는 속성을 정의하고 패키지를 정의 할 수도 있습니다 개인적으로, 나는 "annotatedPackages"를 테스트하지 않았습니다. 그러나 "annotatedClasses"는 완벽하게 작동합니다. @Vlad Mihalcea가 제안한 것처럼, 엔티티는 싱글 톤을 의미하지 않습니다. 그들은 더 "로컬"범위이며 "요청"당 초기화됩니다.

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

    3.규칙에 따라이를 구성 할 수 있습니다. servlet.xml에서 클래스 경로 검색을 수행하는 bean을 추가 할 수 있으며 정규 표현식과 일반적인 명명법을 사용하여 @Component를 자동으로 추가 할 수 있습니다. 자세한 내용은 여기를 참조하십시오.

    규칙에 따라이를 구성 할 수 있습니다. servlet.xml에서 클래스 경로 검색을 수행하는 bean을 추가 할 수 있으며 정규 표현식과 일반적인 명명법을 사용하여 @Component를 자동으로 추가 할 수 있습니다. 자세한 내용은 여기를 참조하십시오.

    http://www.mkyong.com/spring/spring-filtering-components-in-auto-scanning/

  4. from https://stackoverflow.com/questions/27081472/component-hibernate-classes by cc-by-sa and MIT license