我已经看过很多关于这个主题的帖子,但我似乎无法修复它。我面临的问题是我似乎无法在我的数据库中插入数据(postgres)。
表类型
我想添加另一行(即YouTube)
输入 - 型号
@Entity
@Table(name = "types")
public class Type {
// region: parameters
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name", unique = true)
private String name;
// endregion: parameters
// region: relationships
@JsonIgnore
@ManyToMany(mappedBy = "types")
private Set<License> licenses;
// endregion: relationships
// region: constructors
public Type() {
}
public Type(String name, Set<License> licenses) {
this.name = name;
this.licenses = licenses;
}
// endregion: constructors
// region: getters & setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<License> getLicenses() {
return licenses;
}
public void setLicenses(Set<License> licenses) {
this.licenses = licenses;
}
// endregion: getters & setters
}
TypeServiceImpl - 服务
@Service
public class TypeServiceImpl implements TypeService {
private final TypeRepository typeRepository;
@Autowired
public TypeServiceImpl(TypeRepository typeRepository) {
this.typeRepository = typeRepository;
}
public Iterable<Type> list() {
return typeRepository.findAll();
}
public boolean existByName(String name) {
return typeRepository.existByName(name);
}
@Transactional
public Type save(Type type) {
return typeRepository.save(type);
}
}
implements TypeService
目前只是一个空接口。
TypeRepository - dao
@Repository
public interface TypeRepository extends CrudRepository<Type, Integer> {
@Query("SELECT CASE WHEN COUNT (t) > 0 THEN true ELSE false END FROM Type t WHERE t.name = :name")
boolean existByName(@Param("name") String name);
}
TypeController - restcontroller
@RestController
public class TypeController {
private final TypeServiceImpl typeService;
@Autowired
public TypeController(TypeServiceImpl typeService) {
this.typeService = typeService;
}
@RequestMapping(method = RequestMethod.GET, path = "/types")
public Iterable<Type> getAllTypes() {
return typeService.list();
}
@RequestMapping(method = RequestMethod.POST, path = "/types")
public Type createRole(@RequestBody Type type) {
return typeService.save(type);
}
}
每当我使用Json正文 / types 发布时
{
"name": "Youtube"
}
它返回:
{
"id": null,
"name": "Youtube"
}
当我回头看桌子时,它没有被插入。
发布到/ types后记录
19:00:21.619 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing POST request for [/types]
19:00:21.625 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /types
19:00:21.634 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public com.hitmax.server.mvc.model.Type com.hitmax.server.mvc.controller.TypeController.createRole(com.hitmax.server.mvc.model.Type)]
19:00:21.634 [http-nio-8080-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'typeController'
19:00:21.926 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read [class com.hitmax.server.mvc.model.Type] as "application/json" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@1206e027]
19:00:21.984 [http-nio-8080-exec-1] DEBUG org.springframework.core.annotation.AnnotationUtils - Failed to meta-introspect annotation [interface org.springframework.web.bind.annotation.RequestBody]: java.lang.IllegalArgumentException: Annotation must not be null
19:00:22.006 [http-nio-8080-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
19:00:22.021 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Creating new transaction with name [com.hitmax.server.mvc.dao.service.type.TypeServiceImpl.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
19:00:22.021 [http-nio-8080-exec-1] DEBUG org.hibernate.stat.internal.StatisticsInitiator - Statistics initialized [enabled=false]
19:00:22.022 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for Hibernate transaction
19:00:22.033 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]
19:00:22.033 [http-nio-8080-exec-1] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/hitmaxServer]
19:00:22.098 [http-nio-8080-exec-1] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - begin
19:00:22.103 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Exposing Hibernate transaction as JDBC transaction [org.postgresql.jdbc4.Jdbc4Connection@594b5ba9]
19:00:22.139 [http-nio-8080-exec-1] DEBUG org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
19:00:22.143 [http-nio-8080-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
19:00:22.143 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Found thread-bound Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for Hibernate transaction
19:00:22.143 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Participating in existing transaction
19:00:22.165 [http-nio-8080-exec-1] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Opening JPA EntityManager
19:00:22.166 [http-nio-8080-exec-1] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
19:00:22.273 [http-nio-8080-exec-1] DEBUG org.springframework.orm.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
19:00:22.277 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Initiating transaction commit
19:00:22.277 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Committing Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]
19:00:22.277 [http-nio-8080-exec-1] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - committing
19:00:22.278 [http-nio-8080-exec-1] DEBUG org.springframework.orm.hibernate5.HibernateTransactionManager - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] after transaction
19:00:22.396 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Written [com.hitmax.server.mvc.model.Type@268e7743] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@1206e027]
19:00:22.397 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
19:00:22.397 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
任何帮助将不胜感激。
&LT;&GT;
答案 0 :(得分:0)
尝试使用除GenerationType.IDENTITY之外的不同ID生成策略。 使用GenerationType.AUTO做一个简单的测试,看看会发生什么。
答案 1 :(得分:0)
在发布此问题的过程中,我找到了答案。真的很难找到,但我做到了。我仍然发布了这个问题,所以有这个问题的人可能会得到一些东西。
问题
在我的JpaConfiguration文件中,我使用的是HibernateTransactionManager
经理:
@Bean
public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(sessionFactory);
return hibernateTransactionManager;
}
我将其更改为JpaTransactionManager
,这似乎可以解决问题:
@Bean
public JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
return jpaTransactionManager;
}