@组件Hibernate类 [英] @Component Hibernate classes

查看:140
本文介绍了@组件Hibernate类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有冬眠的注释类。由于我正在运行一个Spring项目,我已经将它们包含在servlet.xml文件中(com.student.dto是实际的包名称)并在联系人实体上添加了@Component ..是否有一种方法可以自动添加@Component在所有的hibernate类中。每次我创建一个模型,我最终都会这样做,并觉得应该有更好的方法来做到这一点。

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



$ b @Component
@Entity
@Table(name =Contacts,catalog =javadb)
public class ContactsDTO实现java.io.Serializable {

private int idContacts;
私人StudentDTO StudentDTO;
private String addr1;
private String addr2;
私人字符串城市;
私有字符串状态;
private String pinCode;
私人字符串国家;
私人字符串电话;
私人字符串移动;
私人字符串电子邮件;
private String contactscol;

public ContactsDTO(){
}

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

$ b $ public ContactsDTO(int idContacts,StudentDTO StudentDTO,String addr1,
String addr2,String city,String state,String pinCode,
String country,String phone ,字符串移动,字符串电子邮件,
字符串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 =电子邮件;
this.contactscol = contactscol;
}


getters& setters


解决方案

Spring Beans默认是singleton,你的实体不是线程安全的,它们也不应该是永久的。



实体应该是绑定到持久化上下文的局部变量。它们并不意味着要在多线程环境中访问。

并发控制是处理数据库和您的应用程序逻辑应该主要关注通过防止丢失更新 应用级可重复读取。

您的DAO和服务应该是Spring单例组件。您的实体和请求绑定的DTO不应该是单身。这些对象是短暂的,并且生成它们的请求的范围。



检查 Spring Data JPA docs for solid data access layer design。


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

解决方案

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.

这篇关于@组件Hibernate类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆