对几种模式使用@PersistenceContext和EntityManager [英] Using @PersistenceContext and EntityManager for several schemas

查看:208
本文介绍了对几种模式使用@PersistenceContext和EntityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究我想以最有效的方式构建的JavaSE项目.我的项目是一个employeeManagementSystem,它具有多个模式.例如,我有一个名为employees的架构,它包含员工数据,而另一个有名为company的架构,它包含公司数据.

I am currently working on a JavaSE project that I would like to build in the most effective way. My project is an employeeManagementSystem which has several schemas. For example I have one schema called employees which contains employee data and another schema called company which contains the company data.

当前,我已经实现了我的实体,并使用hibernate.cfg.xml测试了它们,以确保它们正确实现.例如,员工架构中的这样一个实体是

Currently I have implemented my entities and tested these using hibernate.cfg.xml to make sure they are implemented correctly. For example, one such entity in the employee schema is

@Entity
@Table(name="employees", uniqueConstraints= {
        @UniqueConstraint(columnNames="idEmployees"),
        @UniqueConstraint(columnNames="idCardNumber"),
        @UniqueConstraint(columnNames="niNumber")
})
public class Employee {

    @Id
    @GeneratedValue
    @Column(unique=true, nullable=false, updatable=false)
    private int idEmployees;


    @Column(unique=true, nullable=false, updatable=false)
    @Size(min=1, max=15)
    private String idCardNumber;


    @Column(unique=true, nullable=false, updatable=false)
    @Size(min=1, max=15)
    private String  niNumber;

    @Column(nullable=false, updatable=false)
    @Size(min=1, max=20)
    private String name;


    @Column(nullable=false)
    @Size(min=1, max=20)
    private String surname;

    // Other class variables

    //Constructors

    //getters & setters
}

我现在正在尝试使用EntityManager来管理我的模式.我用于员工模式的persistence.xml是

I an now trying to use an EntityManager to manage my schemas. My persistence.xml for the employee schema is

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
             <persistence-unit name="EmployeesDAO" transaction-type="RESOURCE_LOCAL">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <class>com.shopmanagementsystem.employeesdao.entities.Employee</class>
                //Other classes in this schema
                <properties>
                    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                    <property name="javax.persistence.jdbc.user" value="root"/>
                    <property name="javax.persistence.jdbc.password" value="root"/>
                    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employees"/>
                </properties>
             </persistence-unit>
</persistence>

我想创建一个Service和ServiceImpl类来管理与实体相关的过程.

I want to create a Service and ServiceImpl class to manage the processes related to the entities.

Q1)每个实体都应该有一个Service和ServiceImpl吗?

Q1) Should I have a Service and ServiceImpl for each entity?

我的一些问题已经在上一篇帖子 JPA EntityManager构造中得到了回答

Some of my questions were answered already in a previous post JPA EntityManager Construction

Q2)我进一步询问了有关使用@PersistenceContext的问题,并被告知要问另一个问题,对我的项目进行更详细的描述.

Q2) I further asked about using @PersistenceContext and was kindly advised to ask another question with a more detailed description of my project.

如果有什么遗漏,我很乐意添加更多详细信息,感谢您的建议和帮助.

I am happy to add more details if I've left anything out and thank you for your advice and help.

推荐答案

Q1)每个实体我都应该有一个ServiceServiceImpl吗?

Q1) Should I have a Service and ServiceImpl for each entity?

不一定.基于持久性单元的名称:EmployeesDAO,我假设ServiceServiceImp用于实现 DAO模式,对吗?在这种情况下,您必须为那些实际需要合同的实体提供合同(INSERT/UPDATE/DELETE/QUERY).例如,您可以有一个名为Category的表,它与Employee的关系如下:

Not necessarily. Based on the name of the persistence unit: EmployeesDAO, I'm assuming that Service and ServiceImp are intended to implement DAO pattern, right? In this context you have to offer a contract (INSERT / UPDATE/ DELETE / QUERY) just for those entities that actually require it. For example you can have a table called Category which has a relationship with Employee like this:

@Entity
@Table(name="categories")
public class Category implements Serializable {
    ...
    @Id private BigInteger idCategory;
    @Basic private String description;
    ...
}

@Entity
@Table(name="employees")
public class Employee implements Serializable {
    ...
    @ManyToOne
    @JoinColumn(name="idcategory", referencedColumnName="idcategory")
    private Category category;
    ...
}

在此示例中,仅在需要时,您将具有Service来管理Category实体.如果这些实体是由另一个应用程序管理的,那么您只需要对categories表具有读取特权,以便实体管理器可以映射此数据,就是这样.

In this example you would have a Service to manage Category entities only if you need to. If those entities are managed by another application then you just need to have read privileges on categories table so the entity manager can map this data and that's it.

Q2)我进一步询问了有关使用@PersistenceContext的问题,并建议我再问一个问题,提供对我的更详细的描述 项目.

Q2) I further asked about using @PersistenceContext and was kindly advised to ask another question with a more detailed description of my project.

首先请注意 @PersistenceContext 批注旨在与基于容器的持久性一起使用,作为 Java EE 应用程序的一部分.您不能在普通的Java SE平台中使用此注释.

First of all please beware that @PersistenceContext annotation is intended to be used with container-based persistence, as part of a Java EE application. You cannot use this annotation in plain Java SE platform.

另一方面,这并不意味着您不能使用JPA.是的,这是完全有可能的,但是您必须照顾整个实体经理的生命周期.在这种情况下,实现设计模式(例如抽象工厂工厂方法

On the other hand it doesn't mean you can't use JPA. Yes, it's perfectly possible, but you have to take care of the whole entity managers life-cycle. It is a common practice in this case to implement design patterns (such as Abstract Factory or Factory method or Singleton) in order to instantiate/access your entity managers manually.

此外,如果要使用Java SE开发应用程序的顶层并使用容器管理的持久性,则可以使用JPA和

In addition if you want to develop the top level layers of your application using Java SE and use container-managed persistence, then you can code the persistence layer using JPA and Enterprise JavaBeans architecture. This approach is not simple but has several advantages:

  • You can offer the persistence layer as a service through EJB modules.
  • Database connections and pools can be managed by the EJB container. This is extremely useful to separate database connection info from persistence.xml file.
  • As already mentioned, the EJB container will manage the whole entity manager life-cycle.
  • You can go further and leave container use Java Transaction API so you can forget (more or less) about transactional stuff.

我知道发布的persistence.xml可能只是一个例子,但我不得不提到以root用户身份连接到数据库根本不是一个好习惯.出于安全原因,您应该使几位具有正确权限的用户访问您的数据库,而且很可能他们都不会有超级用户权限.

I know that posted persistence.xml is probably just an example but I have to mention that connect to the database as root user is not a good practice at all. Due to security reasons you should have several users with the right privileges to access your database, and likely none of them will ever need super-user privileges.

最后,应用程序设计是一个非常广泛的主题,根据您的要求,它可以是简单的或复杂的.此外,还有几种可以使用的框架/技术,如果您没有任何经验,很容易迷失方向.如果您以前从未做过类似的事情,我的建议是从一个非常基本的应用程序开始,然后再进一步.

Finally, applications design is a really broad topic and it can be simple or complex based on your requirements. In addition there are several frameworks/technologies that can be used and is very very easy to get lost if you don't have any experience. My suggestion if you never did something like this before is to start with a very basic application and then go further.

这篇关于对几种模式使用@PersistenceContext和EntityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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