我有一个依赖的Spring Boot应用程序spring-boot-starter-data-jpa.我的实体类有一个带有列名的列注释.例如:
@Column(name="TestName")
private String testName;
Run Code Online (Sandbox Code Playgroud)
由此生成的SQL创建test_name为列名.在寻找解决方案后,我发现spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy解决了问题(列名取自列注释).
不过,我的问题是为什么没有设置为EJB3NamingStrategyJPA的naming_strategy 忽略列注释?也许hibernate方言与它有关?我正在连接到MS SQL 2014 Express,我的日志包含:
Unknown Microsoft SQL Server major version [12] using SQL Server 2000 dialect
Using dialect: org.hibernate.dialect.SQLServerDialect
Run Code Online (Sandbox Code Playgroud) 我有一个使用许多连接的复杂查询(实际上是8个).我想把它简化成一个视图.经过一番研究后,我可以看到简单性和安全性的好处.但我没有看到任何关于速度的提及.
视图是否像预编译一样工作,其中查询是预编译的?使用视图会有显着的性能提升吗?
我正在尝试使用 Hibernate 处理 OneToMany 关系。我正在使用 @Temporal 注释来告诉 hibernate 有关数据字段的信息。我不知道为什么我会在这里收到此错误。看起来日期格式有问题。请让我知道如何解决它。
顾客
package regular;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Customers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int customer_id;
private String customerName;
private String contactName;
private String address;
private String city;
private String postalCode;
private String country;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "customer_id")
private List<Orders> order;
public Customers() {
}
public …Run Code Online (Sandbox Code Playgroud) 当我尝试在浏览器上运行 URL 以获取所有产品时,我一直收到此“SQLSyntaxErrorException:Unknown column 'product0_.return_policy' in 'field list'” 。
浏览器也显示如下:
出现意外错误(类型=内部服务器错误,状态=500)。无法提取结果集;SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet
returnPolicy是导致此问题的唯一变量。当我从数据库和 Java 中的 Product 类中删除变量本身时,我能够成功地从数据库中检索所有值。
这是作为RESTController一部分的getAllProducts方法:
@RequestMapping(method=RequestMethod.GET, value="/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
Run Code Online (Sandbox Code Playgroud)
当我完全删除 returnPolicy 变量时,它工作正常。
这是 MySQL 表描述:
存储在 returnPolicy 列中的值:
退货政策
0
0
1
1
1
Run Code Online (Sandbox Code Playgroud)
这是“产品”模型变量的代码:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id; …Run Code Online (Sandbox Code Playgroud) 我无法使用带有 Junit5 的 spring boot 2.1.0.M4 和@DataJpaTest. 我正在使用以下 spring crud 存储库界面。
@Repository
public interface DestinationRepository extends CrudRepository<Destination, String> {
Optional<Destination> findCityCode(String code, String cityIsolci);
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试课
package com.test.repository;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.test.Destination;
import java.util.Optional;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
// @ExtendWith(SpringExtension.class)
public class DestinationRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private DestinationRepository destinationRepository;
@Test
public void whenfindByCodeAndCityIsolciThenReturnDestination() throws Exception …Run Code Online (Sandbox Code Playgroud) java ×3
spring-boot ×3
hibernate ×2
mysql ×2
database ×1
jpa ×1
junit5 ×1
optimization ×1
spring ×1
view ×1