Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用entitymanager的正确方法是什么_Java_Hibernate_Jersey_Jax Rs_Jpa 2.1 - Fatal编程技术网

Java 使用entitymanager的正确方法是什么

Java 使用entitymanager的正确方法是什么,java,hibernate,jersey,jax-rs,jpa-2.1,Java,Hibernate,Jersey,Jax Rs,Jpa 2.1,Jersey JAX-RS中有一个REST客户机,它接收请求,然后使用Hibernate的JPA实现从数据库检索数据并返回JSON 使用共享实体管理器,性能非常好,但是如果有多个请求,我会从Hibernate获得异常,主要是NullPointerException 如果我对每个请求使用实体管理器,我不会得到任何异常,但性能较低 处理EntityManager的正确方法是什么 编辑: 在下面的Albert评论之后,我使用了 并将entityManager引用更改为使用entityManager.g

Jersey JAX-RS中有一个REST客户机,它接收请求,然后使用Hibernate的JPA实现从数据库检索数据并返回JSON

使用共享实体管理器,性能非常好,但是如果有多个请求,我会从Hibernate获得异常,主要是NullPointerException

如果我对每个请求使用实体管理器,我不会得到任何异常,但性能较低

处理EntityManager的正确方法是什么

编辑:

在下面的Albert评论之后,我使用了 并将entityManager引用更改为使用entityManager.getEntityManager

e、 g

编辑结束

下面是我使用的代码

Jersey.java

package local.domain;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/testJersey")
public class Jersey {

    static MainClass mainClass = new MainClass();

    @GET
    @Produces("application/json")
    public Response getAccounts(@QueryParam(value = "userId") final int userId) {
        String jsonString = mainClass.getAccounts(userId);
        return Response.ok(jsonString, MediaType.APPLICATION_JSON).build();
    }// getJson


    @Path("/fields/")
    @GET
    @Produces("application/json")
    public Response getAccountFields(@QueryParam(value = "accountId") final int accountId) {
        String jsonString = mainClass.getAccountFields(accountId);
        return Response.ok(jsonString, MediaType.APPLICATION_JSON).build();
    }// getJson
}
MainClass.java

package local.domain;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import local.domain.pojos.Account;
import local.domain.pojos.User;

public class MainClass {
    private EntityManagerFactory entityManagerFactory = null;
    private EntityManager entityManager;

    boolean sharedEntityManager = true;

    public MainClass() {
        entityManagerFactory = Persistence.createEntityManagerFactory("peristenceConfig");
        entityManager = entityManagerFactory.createEntityManager();
    }

    public String getAccounts(int userId) {
        String result = null;
        try {
            User reqUser = null;
            EntityManager entityManagerLocal = null;

            if (sharedEntityManager)
                reqUser = entityManager.find(User.class, userId);
            else {
                entityManagerLocal = entityManagerFactory.createEntityManager();
                reqUser = entityManagerLocal.find(User.class, userId);
            }

            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
            result = gson.toJson(reqUser);
            if (!sharedEntityManager)
                entityManagerLocal.close();
        } catch (Exception exp) {
            System.err.println("Exception with userId"+userId);
            exp.printStackTrace();
        }
        return result;
    }

    public String getAccountFields(int accountId) {
        String result = null;
        try {
            Account reqAccount = null;
            EntityManager entityManagerLocal = null;

            if (sharedEntityManager)
                reqAccount = entityManager.find(Account.class, accountId);
            else {
                entityManagerLocal = entityManagerFactory.createEntityManager();
                reqAccount = entityManagerLocal.find(Account.class, accountId);
            }

            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
            result = gson.toJson(reqAccount);
            if (!sharedEntityManager)
                entityManagerLocal.close();
        } catch (Exception exp) {
            System.err.println("Exception with accountId"+accountId);
            exp.printStackTrace();
        }
        return result;
    }
}
persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="peristenceConfig">
        <class>local.domain.pojos.Account</class>
        <class>local.domain.pojos.AccountField</class>
        <class>local.domain.pojos.User</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpatest" />
            <property name="hibernate.default_schema" value="jpatest" />
            <property name="hibernate.connection.username" value="username" />
            <property name="hibernate.connection.password" value="password" />
        </properties>
    </persistence-unit>
</persistence>
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Snippet</groupId>
  <artifactId>Snippet</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
      <jersey.version>2.26-b02</jersey.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.glassfish.jersey</groupId>
              <artifactId>jersey-bom</artifactId>
              <version>${jersey.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
  <dependencies>
      <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet-core</artifactId>
      </dependency>
      <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.6.Final</version>
    </dependency>
  </dependencies>
</project>
POJOs User.java Account.java AccountField.java

package local.domain.pojos;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_account database table.
 * 
 */
@Entity
@Table(name="tbl_account")
@NamedQuery(name="Account.findAll", query="SELECT a FROM Account a")
public class Account implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="account_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int accountId;

    @Column(name="account_status")
    @Expose
    private String accountStatus;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="user_id")
    private User tblUser;

    //bi-directional many-to-one association to AccountField
    @OneToMany(mappedBy="tblAccount")
    @Expose
    private List<AccountField> tblAccountFields;

    public Account() {
    }

    public int getAccountId() {
        return this.accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    public String getAccountStatus() {
        return this.accountStatus;
    }

    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    public User getTblUser() {
        return this.tblUser;
    }

    public void setTblUser(User tblUser) {
        this.tblUser = tblUser;
    }

    public List<AccountField> getTblAccountFields() {
        return this.tblAccountFields;
    }

    public void setTblAccountFields(List<AccountField> tblAccountFields) {
        this.tblAccountFields = tblAccountFields;
    }

    public AccountField addTblAccountField(AccountField tblAccountField) {
        getTblAccountFields().add(tblAccountField);
        tblAccountField.setTblAccount(this);

        return tblAccountField;
    }

    public AccountField removeTblAccountField(AccountField tblAccountField) {
        getTblAccountFields().remove(tblAccountField);
        tblAccountField.setTblAccount(null);

        return tblAccountField;
    }

}

package local.domain.pojos;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_account_field database table.
 * 
 */
@Entity
@Table(name="tbl_account_field")
@NamedQuery(name="AccountField.findAll", query="SELECT a FROM AccountField a")
public class AccountField implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="account_field_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int accountFieldId;

    @Column(name="account_field_label")
    @Expose
    private String accountFieldLabel;

    //bi-directional many-to-one association to Account
    @ManyToOne
    @JoinColumn(name="account_id")
    private Account tblAccount;

    public AccountField() {
    }

    public int getAccountFieldId() {
        return this.accountFieldId;
    }

    public void setAccountFieldId(int accountFieldId) {
        this.accountFieldId = accountFieldId;
    }

    public String getAccountFieldLabel() {
        return this.accountFieldLabel;
    }

    public void setAccountFieldLabel(String accountFieldLabel) {
        this.accountFieldLabel = accountFieldLabel;
    }

    public Account getTblAccount() {
        return this.tblAccount;
    }

    public void setTblAccount(Account tblAccount) {
        this.tblAccount = tblAccount;
    }

}

package local.domain.pojos;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_user database table.
 * 
 */
@Entity
@Table(name="tbl_user")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="user_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int userId;

    @Lob
    @Column(name="user_email")
    @Expose
    private String userEmail;

    @Column(name="user_last_login")
    @Expose
    private Timestamp userLastLogin;

    @Column(name="user_name")
    @Expose
    private String userName;

    //bi-directional many-to-one association to Account
    @OneToMany(mappedBy="tblUser")
    @Expose
    private List<Account> tblAccounts;

    public User() {
    }

    public int getUserId() {
        return this.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserEmail() {
        return this.userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public Timestamp getUserLastLogin() {
        return this.userLastLogin;
    }

    public void setUserLastLogin(Timestamp userLastLogin) {
        this.userLastLogin = userLastLogin;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<Account> getTblAccounts() {
        return this.tblAccounts;
    }

    public void setTblAccounts(List<Account> tblAccounts) {
        this.tblAccounts = tblAccounts;
    }

    public Account addTblAccount(Account tblAccount) {
        getTblAccounts().add(tblAccount);
        tblAccount.setTblUser(this);

        return tblAccount;
    }

    public Account removeTblAccount(Account tblAccount) {
        getTblAccounts().remove(tblAccount);
        tblAccount.setTblUser(null);

        return tblAccount;
    }

}

正确的方法可能因应用程序类型而异。在您的情况下,我认为共享EntityManager将导致您已经体验到的不想要的行为

我认为每个请求的方法是一种安全的实现方法。另一种可能是对每个线程使用EntityManager,使用ThreadLocal来管理它,Spring就是这样做的。但这可能会使您的实现更加复杂

编辑:每个线程使用EntityManager在使用NIO处理同一线程内的多个请求时可能会导致问题

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Snippet</groupId>
  <artifactId>Snippet</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
      <jersey.version>2.26-b02</jersey.version>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.glassfish.jersey</groupId>
              <artifactId>jersey-bom</artifactId>
              <version>${jersey.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
    <repositories>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
  <dependencies>
      <dependency>
          <groupId>org.glassfish.jersey.containers</groupId>
          <artifactId>jersey-container-servlet-core</artifactId>
      </dependency>
      <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.6.Final</version>
    </dependency>
  </dependencies>
</project>
package local.domain.pojos;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_account database table.
 * 
 */
@Entity
@Table(name="tbl_account")
@NamedQuery(name="Account.findAll", query="SELECT a FROM Account a")
public class Account implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="account_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int accountId;

    @Column(name="account_status")
    @Expose
    private String accountStatus;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="user_id")
    private User tblUser;

    //bi-directional many-to-one association to AccountField
    @OneToMany(mappedBy="tblAccount")
    @Expose
    private List<AccountField> tblAccountFields;

    public Account() {
    }

    public int getAccountId() {
        return this.accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    public String getAccountStatus() {
        return this.accountStatus;
    }

    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    public User getTblUser() {
        return this.tblUser;
    }

    public void setTblUser(User tblUser) {
        this.tblUser = tblUser;
    }

    public List<AccountField> getTblAccountFields() {
        return this.tblAccountFields;
    }

    public void setTblAccountFields(List<AccountField> tblAccountFields) {
        this.tblAccountFields = tblAccountFields;
    }

    public AccountField addTblAccountField(AccountField tblAccountField) {
        getTblAccountFields().add(tblAccountField);
        tblAccountField.setTblAccount(this);

        return tblAccountField;
    }

    public AccountField removeTblAccountField(AccountField tblAccountField) {
        getTblAccountFields().remove(tblAccountField);
        tblAccountField.setTblAccount(null);

        return tblAccountField;
    }

}

package local.domain.pojos;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_account_field database table.
 * 
 */
@Entity
@Table(name="tbl_account_field")
@NamedQuery(name="AccountField.findAll", query="SELECT a FROM AccountField a")
public class AccountField implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="account_field_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int accountFieldId;

    @Column(name="account_field_label")
    @Expose
    private String accountFieldLabel;

    //bi-directional many-to-one association to Account
    @ManyToOne
    @JoinColumn(name="account_id")
    private Account tblAccount;

    public AccountField() {
    }

    public int getAccountFieldId() {
        return this.accountFieldId;
    }

    public void setAccountFieldId(int accountFieldId) {
        this.accountFieldId = accountFieldId;
    }

    public String getAccountFieldLabel() {
        return this.accountFieldLabel;
    }

    public void setAccountFieldLabel(String accountFieldLabel) {
        this.accountFieldLabel = accountFieldLabel;
    }

    public Account getTblAccount() {
        return this.tblAccount;
    }

    public void setTblAccount(Account tblAccount) {
        this.tblAccount = tblAccount;
    }

}

package local.domain.pojos;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.google.gson.annotations.Expose;


/**
 * The persistent class for the tbl_user database table.
 * 
 */
@Entity
@Table(name="tbl_user")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="user_id")
    @Expose
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int userId;

    @Lob
    @Column(name="user_email")
    @Expose
    private String userEmail;

    @Column(name="user_last_login")
    @Expose
    private Timestamp userLastLogin;

    @Column(name="user_name")
    @Expose
    private String userName;

    //bi-directional many-to-one association to Account
    @OneToMany(mappedBy="tblUser")
    @Expose
    private List<Account> tblAccounts;

    public User() {
    }

    public int getUserId() {
        return this.userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserEmail() {
        return this.userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public Timestamp getUserLastLogin() {
        return this.userLastLogin;
    }

    public void setUserLastLogin(Timestamp userLastLogin) {
        this.userLastLogin = userLastLogin;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<Account> getTblAccounts() {
        return this.tblAccounts;
    }

    public void setTblAccounts(List<Account> tblAccounts) {
        this.tblAccounts = tblAccounts;
    }

    public Account addTblAccount(Account tblAccount) {
        getTblAccounts().add(tblAccount);
        tblAccount.setTblUser(this);

        return tblAccount;
    }

    public Account removeTblAccount(Account tblAccount) {
        getTblAccounts().remove(tblAccount);
        tblAccount.setTblUser(null);

        return tblAccount;
    }

}