我们@RepositoryRestResource在PagingAndSortingRepository连接到postgres数据库的顶部使用了一个非常简单的设置.我们还配置spring.jackson.property-naming-strategy=SNAKE_CASE了返回漂亮的json.在我们开始排序之前,这一切都很好,很花哨.正如我们所发现的 - 排序要求我们提供实际的类字段名称(我们当然在驼峰的情况下):
get("/thing?sort=dateCreated,desc")
当我们尝试javascript友好
get("/thing?sort=date_created,desc")
它失败了,因为jpa试图用下划线拆分参数.
是否有一种简单的方法让路径params与我们在json中返回的格式相同?
我想要实现的是生成一个在数据库插入期间自动分配的UUID.类似于名为"id"的主键列生成id值.
模型值看起来像这样:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private Long id;
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "uuid", columnDefinition = "BINARY(16)")
private UUID uuid;
Run Code Online (Sandbox Code Playgroud)
但是当DB插入完成时."uuid"是空的.
非常感谢帮助.如果我问一个明显的愚蠢问题,我很抱歉.
我无法理解我使用JPA存储库的简单Spring MVC项目出了什么问题.能否请你提一下.
领域:
package com.test.app;
@Entity
@Table(name = "foo_table")
public class FooDomain {
@Id
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "text", nullable = false)
private String text;
// getters & setters here...
Run Code Online (Sandbox Code Playgroud)
}
知识库
package com.test.app;
@RepositoryDefinition(domainClass=FooDomain.class, idClass=Long.class)
public interface FooRepository extends CrudRepository<FooDomain, Long> {}
Run Code Online (Sandbox Code Playgroud)
调节器
@Controller
public class HomeController {
@Autowired
private FooRepository fooRepository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
model.addAttribute("rowsNumber", fooRepository.count());
return …Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我使用Hibernate与SQL Server数据库,所以我设置
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect">
Run Code Online (Sandbox Code Playgroud)
在我的persistence.xml中.
在某些情况下,我想用NULL包括排序记录,我使用关键字NULLS FIRST.
因为Hibernate中的CriteriaQuery/CriteriaBuilder默认不支持它,所以我使用Interceptor来修改本机查询.
问题是,SQL Server不支持关键字NULLS FIRST,因此我使用关键字:
case when column_name is null then 0 else 1 end, column_name
Run Code Online (Sandbox Code Playgroud)
如果我想将数据库从SQL Server迁移到Oracle(例如),那么我需要在我的拦截器中放置if-else,选择我正在使用哪种方言,对吧?
这就是我说明它们的方式:
String dialect = ..............
if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) { // get SQL Server dialect
// put keyword "case when column_name is null then 0 else 1 end, column_name"
} else {
// put keyword "NULLS FIRST/LAST"
}
Run Code Online (Sandbox Code Playgroud)
如何在运行时获取方言配置(在persistence.xml中)?
我对Spring JPA有点新意,所以如果我的问题听起来很基本,我会提前道歉.我有2个实体对象:OrderInfo和PersonInfo.课程如下:
@Entity
@Table(name="order_info")
@NamedQuery(name="OrderInfo.findAll", query="SELECT o FROM OrderInfo o")
public class OrderInfo implements Serializable {
@Column(name="order_number")
private String orderNumber;
//bi-directional many-to-one association to PersonInfo
@ManyToOne
@JoinColumn(name="person_id")
private PersonInfo personInfo;
public String getOrderNumber() {
return this.orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public PersonInfo getPersonInfo() {
return this.personInfo;
}
public void setPersonInfo(PersonInfo personInfo) {
this.personInfo = personInfo;
}
}
Run Code Online (Sandbox Code Playgroud)
而Person实体:
@Entity
@Table(name="person_info")
@NamedQuery(name="PersonInfo.findAll", query="SELECT p FROM PersonInfo p")
public class PersonInfo implements Serializable {
//bi-directional many-to-one …Run Code Online (Sandbox Code Playgroud) 我的问题是关于混合程序化和声明式事务可能出现的并发问题.我正在开发一种遗留软件(Spring + Hibernate),它以编程方式处理数据库连接和事务.
Session db = HibernateUtil.getSessionFactory().openSession();
db.beginTransaction();
// do stuff
db.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
该软件具有较新的模块,这些模块使用Spring数据架构和声明式事务(@Transactional).当从"手动"打开的事务内部调用较新的Spring服务时,我们在极少数情况下使用Microsoft SQL Server遇到数据库死锁.我认为问题是有两个嵌套事务读/写同一个表导致死锁.
Session db = HibernateUtil.getSessionFactory().openSession();
db.beginTransaction();
// do stuff
springService.getStuff();
// do stuff
db.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
有没有办法安全地混合这些交易或在两者中使用已经开始的交易?我应该在调用Spring @ Service/@ Repository方法之前关闭手动/以编程方式打开的事务吗?Spring和HibernateUtil都使用相同的实体管理器进行数据库连接.
我有一个名为 Task 的实体,它有 taskId 字段。问题是我需要创建/更新一些特定任务,而 JPA 自动生成器不允许我手动设置任务 ID。(它会覆盖我的任务 ID)
有没有办法手动设置taskId?
@Id
@Column(name = "task_id")
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private String taskId;
Run Code Online (Sandbox Code Playgroud) 现在我正在创建EntityManagerFactory这样的:
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put(DRIVER, "com.mysql.jdbc.Driver")
.put(DIALECT, "org.hibernate.dialect.MySQL5Dialect");
.put(USER, dbUsername)
.put(PASS, dbPassword)
.put(URL, dbConnectionUrl)
//Some more properties
.build();
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.configure(properties);
cfg.addAnnotatedClass(AuditEntry.class);
cfg.addAnnotatedClass(LastWrittenEventId.class);
//Some more annotated classes
return cfg.createEntityManagerFactory();
Run Code Online (Sandbox Code Playgroud)
但是正如我在javadocs中看到的那样,Ejb3Configuration不推荐使用它,我不应该使用它.我应该Persistence.createEntityManagerFactory()按照JPA规范 7.3节使用.但是后来我只能传递一些属性,但是我可以以某种方式添加带注释的类吗?
我有一个属性类型的JPA实体java.time.LocalDateTime.我使用javax.persistence.Converter注释来实现这一点.我可以加载实体并保存它没有问题,但当我尝试执行这样的jpql查询时:
TypedQuery<Event> q = em.createQuery(
"SELECT e " +
"FROM Event e " +
"WHERE :currentDateTime >= e.startDateTime", Event.class);
q.setParameter("currentDateTime", LocalDateTime.now().withSecond(0).withNano(0));
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bytea >= timestamp without time zone
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,但无法使参数起作用.我甚至尝试使用java.util.Date和java.util.Calendar作为参数,但它也不起作用.
我试图说服"更高层次"使用querydsl sql来保持持久性.但是他们更喜欢spring jdbctemplate,原因是它提供了最好的原始性能.
表现是我们对课程的首要要求.这就是为什么JPA根本不是一个选择.QueryDSL SQL开销是否过多,无法从我们的选项中解脱出来?
我想知道是否有任何"最近的"性能测试来显示querydsl sql如何使用jdbctemplate和jpa.
我遇到过这个.我想知道querydsl sql与jdbctemplate和jpa实现进行比较时的相对性能.