2

I have hibernated annotated classes in my program. Since I'm running a Spring project, I have included them in the servlet.xml file(com.student.dto is the actual package name) and added @Component on the Contacts Entity..Is there a way to automate adding @Component on all the hibernate classes..Each time I create a model, I end up doing this and feel there should be a better to do this.

<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
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 3
    whow, model = DTO = spring bean. Seriously.. – energizer Nov 22 '14 at 19:32
  • energizer: The model will not be used by DTO but can you suggest how to initialize the model class without being declared as a @component... – user1050619 Nov 22 '14 at 20:46
  • I think what he meant was generally i don't think entities are annotated with comportment. Generally some sort of business service would be – Dan King Nov 22 '14 at 22:08

3 Answers3

3

You are doing it all wrong. Spring Beans are singleton by default and your entities are not thread safe and neither they should ever be.

Entities should be local variables bound to a Persistent Context. They are not meant to be accessed in a multi-threaded environment.

Concurrency control is handled by the database and your application logic should mostly be concern about preventing lost updates through application-level repeatable reads.

Your DAO and Services should be Spring singleton components. Your Entities and request bound DTOs should never be singleton. These objects are short-lived and scoped to the request that generated them.

Check the Spring Data JPA docs for a solid data access layer design.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • From what I read, Spring Beans are not thread safe as well. If that is the case, how can I make sure my DAO & Services are thread safe. – user1050619 Nov 24 '14 at 04:18
  • Spring Beans should not have mutable state. All state is built at initialization time. The mutable state is encapsulated in request/response data. – Vlad Mihalcea Nov 24 '14 at 05:23
1

The @Component javadocs says this.

Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

The @Component, @Repository etc are Typically used for Auto scanning (during application bootstrap)and for the dependency injection. I dont see a point in making your entity as a Spring Component. The typical use of an entity is that it represents your Relational database Table. Entity (Java)= Table (RDBMS). Here is the definition of an Entity

An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.

The way to include your entities should be something like this:

    <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>

Note that you can also define a property called "annotatedPackages" and define your packages Personally, i havent tested "annotatedPackages". But, "annotatedClasses" work perfectly. As suggested by @Vlad Mihalcea, Entities are not meant to be Singleton. They are more of "Local" scope, and are to be intialized per "request".

Bhaskara
  • 601
  • 9
  • 18
0

You can configure this by convention. In the servlet.xml you can add a bean that does class path scanning and can automatically add the @Component using a regex and a common naming approach. See here for details:

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

Dan King
  • 1,080
  • 1
  • 11
  • 28