Spring Boot JPA Postgres。
日志没有明显异常时,不会保存实体。
实体:TransactionInfo.java
package com.demo.kmd.models;
import java.util.List;
import com.plaid.client.response.TransactionsGetResponse.Transaction.Location;
import com.plaid.client.response.TransactionsGetResponse.Transaction.PaymentMeta;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "spendschema.transaction")
public class TransactionInfo {
@Id
@GeneratedValue
private Long genId; //generated id locally in database .
private String accountId;
private Double amount;
private String isoCurrencyCode;
private String unofficialCurrencyCode;
private String categoryId;
private String date;
private String name;
private String originalDescription;
private Boolean pending;
private String pendingTransactionId;
private String transactionId;
private String transactionType;
private String accountOwner;
public String getTransactionId() {
return transactionId;
}
public String getAccountId() {
return accountId;
}
public Boolean getPending() {
return pending;
}
public String getPendingTransactionId() {
return pendingTransactionId;
}
public String getTransactionType() {
return transactionType;
}
public String getDate() {
return date;
}
public String getName() {
return name;
}
public Double getAmount() {
return amount;
}
}
TransactionJpaReposotory.java
package com.demo.kmd.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import com.demo.kmd.models.TransactionInfo;
@Component
public interface TransactionJpaRepository extends CrudRepository<TransactionInfo, Long>{
}
PersistanceService.java
package com.demo.kmd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.kmd.models.TransactionInfo;
import com.demo.kmd.repository.TransactionJpaRepository;
@Service
public class PersistanceService {
@Autowired
TransactionJpaRepository transactionJpaRepository;
public TransactionInfo insert(TransactionInfo transactionInfo) {
return transactionJpaRepository.save(transactionInfo);
}
}
TestController.java
package com.demo.controller;
import com.demo.kmd.models.TransactionInfo;
....
import retrofit2.Response;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.demo.kmd.service.PersistanceService;
import static org.springframework.web.bind.annotation.RequestMethod.*;
@Controller
public class TestController {
@Autowired
PersistanceService persistanceService;
@RequestMapping(value="/test1", method=GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity getTransactions3() throws Exception {
System.out.print("test hit");
TransactionInfo transactionInfo = new TransactionInfo();
transactionInfo.setAccountId("ACCOUNTID1234");
transactionInfo.setAmount(4000.12);
transactionInfo = persistanceService.insert(transactionInfo);
return ResponseEntity.ok(transactionInfo);
}
Application.properties
logging.level.root=DEBUG
logging.level.org.hibernate=DEBUG
spring.datasource.url=jdbc:postgresql://192.168.0.23:5432/spenddb
spring.datasource.username= spenduser
spring.datasource.password= ***
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto = update`
pgAdmin(postgres)
select * from spendschema.transaction
**No result `**
日志:
019-12-07 10:02:49.406 DEBUG 3628 --- [nio-8080-exec-7] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
test hit2019-12-07 10:02:49.408 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@7fa03c3f] for JPA transaction
2019-12-07 10:02:49.408 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2019-12-07 10:02:49.408 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.t.internal.TransactionImpl : begin
2019-12-07 10:02:49.416 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@7b9fb713]
2019-12-07 10:02:49.418 DEBUG 3628 --- [nio-8080-exec-7] org.hibernate.SQL : select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
2019-12-07 10:02:49.423 DEBUG 3628 --- [nio-8080-exec-7] org.hibernate.id.SequenceGenerator : Sequence identifier generated: BasicHolder[java.lang.Long[16]]
2019-12-07 10:02:49.424 DEBUG 3628 --- [nio-8080-exec-7] o.h.r.j.i.ResourceRegistryStandardImpl : HHH000387: ResultSet's statement was not registered
2019-12-07 10:02:49.424 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.i.AbstractSaveEventListener : Generated identifier: 16, using strategy: org.hibernate.id.SequenceGenerator
2019-12-07 10:02:49.425 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit
2019-12-07 10:02:49.425 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@7fa03c3f]
2019-12-07 10:02:49.425 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.t.internal.TransactionImpl : committing
2019-12-07 10:02:49.425 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.i.AbstractFlushingEventListener : Processing flush-time cascades
2019-12-07 10:02:49.425 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.i.AbstractFlushingEventListener : Dirty checking collections
2019-12-07 10:02:49.426 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.i.AbstractFlushingEventListener : Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
2019-12-07 10:02:49.426 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.i.AbstractFlushingEventListener : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2019-12-07 10:02:49.426 DEBUG 3628 --- [nio-8080-exec-7] o.hibernate.internal.util.EntityPrinter : Listing entities:
2019-12-07 10:02:49.426 DEBUG 3628 --- [nio-8080-exec-7] o.hibernate.internal.util.EntityPrinter : com.demo.kmd.models.TransactionInfo{date=null, amount=4000.12, pending=null, accountOwner=null, transactionId=null, genId=16, transactionType=null, accountId=ACCOUNTID1234, name=null, unofficialCurrencyCode=null, pendingTransactionId=null, isoCurrencyCode=null, categoryId=null, originalDescription=null}
2019-12-07 10:02:49.427 DEBUG 3628 --- [nio-8080-exec-7] org.hibernate.SQL : insert into spendschema_transaction (account_id, account_owner, amount, category_id, date, iso_currency_code, name, original_description, pending, pending_transaction_id, transaction_id, transaction_type, unofficial_currency_code, gen_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into spendschema_transaction (account_id, account_owner, amount, category_id, date, iso_currency_code, name, original_description, pending, pending_transaction_id, transaction_id, transaction_type, unofficial_currency_code, gen_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2019-12-07 10:02:49.437 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.JpaTransactionManager : Not closing pre-bound JPA EntityManager after transaction
2019-12-07 10:02:49.440 DEBUG 3628 --- [nio-8080-exec-7] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [com.demo.kmd.models.TransactionInfo@3e9d3604] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@129ecd4a]
2019-12-07 10:02:49.441 DEBUG 3628 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2019-12-07 10:02:49.441 DEBUG 3628 --- [nio-8080-exec-7] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2019-12-07 10:02:49.441 DEBUG 3628 --- [nio-8080-exec-7] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager
2019-12-07 10:02:49.441 DEBUG 3628 --- [nio-8080-exec-7] o.h.e.jdbc.internal.JdbcCoordinatorImpl : HHH000420: Closing un-released batch
2019-12-07 10:02:49.442 DEBUG 3628 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
2019-12-07 10:02:49.442 DEBUG 3628 --- [nio-8080-exec-7] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@9687fb6
2019-12-07 10:02:49.443 DEBUG 3628 --- [nio-8080-exec-7] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7167dc63:org.apache.tomcat.util.net.NioChannel@1ba33e7e:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:53851]], Read from buffer: [0]
2019-12-07 10:02:49.443 DEBUG 3628 --- [nio-8080-exec-7] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@7167dc63:org.apache.tomcat.util.net.NioChannel@1ba33e7e:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:53851]], Status in: [OPEN_READ], State out: [OPEN]
2019-12-07 10:03:14.811 DEBUG 3628 --- [nio-8080-exec-8] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@1f62c78:org.apache.tomcat.util.net.NioChannel@30a5cb21:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:53852]], Read from buffer: [0]
2019-12-07 10:03:14.812 DEBUG 3628 --- [nio-8080-exec-8] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header
java.io.EOFException: null
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1250) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1190) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:717) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:366) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:687) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_181]
2019-12-07 10:03:14.813 DEBUG 3628 --- [nio-8080-exec-8] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@1f62c78:org.apache.tomcat.util.net.NioChannel@30a5cb21:java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:8080 remote=/0:0:0:0:0:0:0:1:53852]], Status in: [OPEN_READ], State out: [CLOSED]
最佳答案
问题是您正在使用new关键字创建TransactionInfo transactionInfo = new TransactionInfo();
,而它应该是@Autowired,因为它是Spring驱动的服务bean。