我在使用jpa和mariadb调用存储过程时遇到NullPointerException。
第一次工作正常,当我第二次尝试时,它会给出这个例外。
当我重新启动服务器时,它第一次再次工作。
我使用的是春季靴1.5.6。
maven依赖:
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.5.7</version>
</dependency>
这些是application.properties
spring.datasource.url=jdbc:mariadb://localhost:3306/webapp
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driverclassName=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
Java代码
@Override
public User register(User user) {
StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("webapp.register")
.registerStoredProcedureParameter(1, String.class, ParameterMode.IN).setParameter(1, user.getEmail())
.registerStoredProcedureParameter(2, String.class, ParameterMode.IN).setParameter(2, user.getPassword())
.registerStoredProcedureParameter(3, String.class, ParameterMode.IN).setParameter(3, user.getName())
.registerStoredProcedureParameter(4, String.class, ParameterMode.IN).setParameter(4, "")
.registerStoredProcedureParameter(5, String.class, ParameterMode.IN).setParameter(5, "")
.registerStoredProcedureParameter(6, Long.class, ParameterMode.OUT);
storedProcedureQuery.execute();
long userId = (long)storedProcedureQuery.getOutputParameterValue(6);
user.setUserId(userId);
return user;
}
我在第storedProcedureQuery.execute();
行得到的例外是
Hibernate: {call webapp.register(?,?,?,?,?,?)}
java.lang.NullPointerException: null
at org.mariadb.jdbc.MariaDbServerPreparedStatement.prepare(MariaDbServerPreparedStatement.java:143) ~[mariadb-java-client-1.5.7.jar:na]
at org.mariadb.jdbc.MariaDbServerPreparedStatement.clone(MariaDbServerPreparedStatement.java:134) ~[mariadb-java-client-1.5.7.jar:na]
at org.mariadb.jdbc.AbstractCallableProcedureStatement.clone(AbstractCallableProcedureStatement.java:98) ~[mariadb-java-client-1.5.7.jar:na]
at org.mariadb.jdbc.MariaDbProcedureStatement.clone(MariaDbProcedureStatement.java:119) ~[mariadb-java-client-1.5.7.jar:na]
at org.mariadb.jdbc.MariaDbProcedureStatement.clone(MariaDbProcedureStatement.java:62) ~[mariadb-java-client-1.5.7.jar:na]
at org.mariadb.jdbc.MariaDbConnection.prepareCall(MariaDbConnection.java:479) ~[mariadb-java-client-1.5.7.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126) ~[tomcat-jdbc-8.5.16.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.16.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:75) ~[tomcat-jdbc-8.5.16.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.16.jar:na]
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81) ~[tomcat-jdbc-8.5.16.jar:na]
at com.sun.proxy.$Proxy80.prepareCall(Unknown Source) ~[na:na]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:86) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]