gpt4 book ai didi

java - Spring 启动: Reading data using Spring Data JPA returns incorrect values

转载 作者:行者123 更新时间:2023-12-01 08:59:55 25 4
gpt4 key购买 nike

我在我的应用程序中使用 Spring Boot。我有一个访问服务层的 Controller ,该服务层访问 Spring Data JPA 的 CrudRepository 的实现,该实现与 Hibernate 交互以从数据库获取数据。 CrudRepository 的以下所有三个实现都返回错误的对象:

public class ProductRepositoryImpl implements ProductRepository {

@Autowired
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public List<Product> findProductBatch(int start, int end) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Product");
query.setFirstResult(start);
query.setMaxResults(end);
List<Product> list = query.list();
return list;
}

@SuppressWarnings({ "unused", "unchecked" })
@Override
public Iterable<Product> findAll() {
Session session = sessionFactory.getCurrentSession();
List<Product> products = session.createQuery("from Product").list();
Iterable<Product> productIterable = products;
return products;
}

@SuppressWarnings("unchecked")
public List<Product> findBatch(int start, int end) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Product");
query.setFirstResult(start);
query.setMaxResults(end);
List<Product> list = query.list();
return list;
}

}

仅当传递给 findOne() 方法的参数为零且记录的 id 不为零时,findOne() 方法才会返回一条记录。发送请求时返回以下 JSON 响应:

{
"productId": 0,
"name": "xxxxxx",
"productCategoryId": null,
"created": 1482655958000,
"createdBy": null
}

该记录存在于数据库中,但其 id 为 1,并且确实有一个“productCategoryId”,但由于未注入(inject),因此返回 null。

findAll() 方法返回重复多次的同一记录,findBatch() 方法返回类似的响应。

这是其余的代码:

产品.java:

@Entity
public class Product {

@Id
private Integer productId;
private String name;
private Integer productCategoryId;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "productCategoryId", insertable = false, updatable = false)
@Autowired
private ProductCategory productCategory;
private Date created;
private Integer createdBy;

public Product() {
}

public Product(Integer productId, String name, ProductCategory productCategory, Integer uomId, Date created, Integer createdBy) {
this.productId = productId;
this.name = name;
this.productCategory = productCategory;
this.created = created;
this.createdBy = createdBy;
}

public Product(String name, ProductCategory productCategory) {
this.name = name;
this.productCategory = productCategory;
}

public Integer getProductId() {
return productId;
}

public void setProductId(Integer productId) {
this.productId = productId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getProductCategoryId() {
return productCategoryId;
}

public void setProductCategoryId(Integer productCategoryId) {
this.productCategoryId = productCategoryId;
}

// public ProductCategory getProductCategory() {
// return productCategory;
// }
//
// public void setProductCategory(ProductCategory productCategory) {
// this.productCategory = productCategory;
// }

public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

public Integer getCreatedBy() {
return createdBy;
}

public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
}

产品类别.java:

    @Entity
public class ProductCategory {

@Id
private Integer productCategoryId;
private String name;
private Date created;
private Integer createdBy;

public ProductCategory() {
}

public ProductCategory(Integer productCategoryId, String name, Date created, Integer createdBy) {
this.productCategoryId = productCategoryId;
this.name = name;
this.created = created;
this.createdBy = createdBy;
}

public Integer getProductCategoryId() {
return productCategoryId;
}

public void setProductCategoryId(Integer productCategoryId) {
this.productCategoryId = productCategoryId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

public Integer getCreatedBy() {
return createdBy;
}

public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
}

ProductServiceImpl.java

@Service
@Transactional
public class ProductServiceImpl implements ProductService {



@Autowired
private ProductRepository productRepository;

public Product findOne(int productId) {
return productRepository.findOne(productId);
}

public List<Product> findAll() {
List<Product> products = new ArrayList<>();
productRepository.findAll().forEach(products::add);
return products;
}

@Override
public List<Product> findBatch(int start, int end) {
return productRepository.findBatch(start, end);
}
}

ProductController.java:

@RestController
@RequestMapping("/products")
@EnableTransactionManagement
public class ProductController {

@Autowired
private ProductService productService;

@RequestMapping(method = RequestMethod.GET)
public List<Product> findAll() {
List<Product> products = productService.findAll();
return products;
}

@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public Product findOne(@PathVariable int productId) {
Product product = productService.findOne(productId);
return product;
}

@RequestMapping(value = "/{start}/{end}", method = RequestMethod.GET)
public List<Product> findBatch(@PathVariable int start, @PathVariable int end) {
List<Product> list = productService.findBatch(start, end);
return list;
}
}

ApplicationContextConfig.java:

public class ApplicationContextConfig {

@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
}

响应还应包含完整注入(inject)的 ProductCategory 对象,但事实并非如此。

最佳答案

最后,解决方案是将其添加到我的 application.properties 文件中:

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

第一行检查列是否存在而不更改其结构,而第二行更改 Hibernate 验证的默认命名策略并避免在表名称中添加下划线。

关于java - Spring 启动: Reading data using Spring Data JPA returns incorrect values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41768135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com