JPA返回抛出内部服务器错误的对象

时间:2017-12-12 19:51:12

标签: java mysql jpa jax-rs

我正在使用JAX-RS和MySql数据库(JPA)构建API。 我有以下方法验证用户名和密码并为会话创建令牌:

   <build>
        <finalName>desired_jar_name</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>my.main.class</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

此方法由以下方式调用和使用:

public Customer validateCustomer(Customer customer) {

    //get the customer from the database
    Query q = em.createQuery("SELECT c from Customer c WHERE c.login = :username");
    q.setParameter("username", customer.getLogin());
    List<Customer> results = q.getResultList();

    //check if user exsists
    if(!results.isEmpty()){
       Customer fromDb = results.get(0);

       //check if user provided correct password
       if (!fromDb.getPassword().equals(customer.getPassword())) {
           return null;
       } else {
           //create the session token and save it to the database
           Random random = new SecureRandom();
           String token = new BigInteger(130, random).toString(32);
           tx.begin();
           int executeUpdate = em.createQuery("UPDATE Customer c SET c.token = :token WHERE c.id = :id")
                   .setParameter("token", token)
                   .setParameter("id", fromDb.getId())
                   .executeUpdate();
           tx.commit();
           em.close();

           //update previously pulled customer with the token
           fromDb.setToken(token);

           //return the customer
           return fromDb;
       }
    } else {
        return null;
    }
}

我的问题是每当提供正确的登录时我都会返回

  

从Db返回;

我在浏览器中收到内部服务器错误500:

  

HTTP ERROR 500

     

访问/ api / users / login时出现问题。原因是:

     

内部服务器错误

     

由Jetty提供://

但是,当我从数据库返回原始客户时,只会出现错误。如果我从数据库返回的同一个对象构建新的Customer对象并返回它,它工作得很好。

public Response login (Customer customer){
    Customer c = customerService.validateCustomer(customer);
    if (c != null){
        return Response.status(Response.Status.OK).entity(c).build();
        //System.out.println(c);
    } else {
        return Response.status(Response.Status.FORBIDDEN).entity("Incorrect username or password.").build();
    }
}

返回c代替fromDb可以正常工作。

是否有任何理由说明数据库中的对象可能返回与构建它的对象不同的结果?

从DB2返回时的服务器日志(获取错误):

Customer c = new Customer();
           c.setEmail(fromDb.getEmail());
           c.setLogin(fromDb.getLogin());
           c.setToken(token);
           c.setId(fromDb.getId());

返回c时出现错误日志(无错误)

 INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)  
 INFO: HHH000115: Hibernate connection pool size: 20  
 INFO: HHH000006: Autocommit mode: true  
 INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]  
 INFO: HHH000046: Connection properties: {user=root, autocommit=true, release_mode=auto}  
 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect  
 INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory  
 INFO: HHH000397: Using ASTQueryTranslatorFactory  
 INFO: HHH000228: Running hbm2ddl schema update  
 INFO: HHH000102: Fetching database metadata  
 INFO: HHH000396: Updating schema  
 INFO: HHH000261: Table found: test.account  
 INFO: HHH000037: Columns: [balance, name, id, accnumber, sortcode]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: [primary]  
 INFO: HHH000261: Table found: test.customer  
 INFO: HHH000037: Columns: [password, city, street, county, name, id, login, email, token]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: [primary]  
 INFO: HHH000261: Table found: test.customer_account  
 INFO: HHH000037: Columns: [account_id, customer_id]  
 INFO: HHH000108: Foreign keys: [fk70eacecc22b4f18d, fk70eacecc3a281067]  
 INFO: HHH000126: Indexes: [fk70eacecc22b4f18d, fk70eacecc3a281067, account_id]  
 INFO: HHH000261: Table found: test.hibernate_sequences  
 INFO: HHH000037: Columns: [sequence_next_hi_value, sequence_name]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: []  
 INFO: HHH000232: Schema update complete  
 WARN: HHH000436: Entity manager factory name (Bank) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'  
 INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)  
 INFO: HHH000115: Hibernate connection pool size: 20  
 INFO: HHH000006: Autocommit mode: true  
 INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]  
 INFO: HHH000046: Connection properties: {user=root, autocommit=true, release_mode=auto}  
 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect  
 INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory  
 INFO: HHH000397: Using ASTQueryTranslatorFactory  
 INFO: HHH000228: Running hbm2ddl schema update  
 INFO: HHH000102: Fetching database metadata  
 INFO: HHH000396: Updating schema  
 INFO: HHH000261: Table found: test.account  
 INFO: HHH000037: Columns: [balance, name, id, accnumber, sortcode]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: [primary]  
 INFO: HHH000261: Table found: test.customer  
 INFO: HHH000037: Columns: [password, city, street, county, name, id, login, email, token]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: [primary]  
 INFO: HHH000261: Table found: test.customer_account  
 INFO: HHH000037: Columns: [account_id, customer_id]  
 INFO: HHH000108: Foreign keys: [fk70eacecc22b4f18d, fk70eacecc3a281067]  
 INFO: HHH000126: Indexes: [fk70eacecc22b4f18d, fk70eacecc3a281067, account_id]  
 INFO: HHH000261: Table found: test.hibernate_sequences  
 INFO: HHH000037: Columns: [sequence_next_hi_value, sequence_name]  
 INFO: HHH000108: Foreign keys: []  
 INFO: HHH000126: Indexes: []  
 INFO: HHH000232: Schema update complete  
 WARN: HHH000436: Entity manager factory name (Bank) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'  
 com.mycompany.bank.model.Customer@769f10ad  
 Returning: com.mycompany.bank.model.Customer@e501b7e  

1 个答案:

答案 0 :(得分:0)

首先,您的查询不正常:您无法在更新期间使用别名。试试em.createQuery("UPDATE Customer SET token = :token WHERE id = :id")