如何将自定义sql附加到Spring Data JPA插入/更新

时间:2018-03-04 19:42:12

标签: spring db2 spring-data spring-data-jpa db2-400

根据IBM's docs,为了让我进行插入/更新,我需要附加一些sql(with NC OR with NONE)来更新和插入语句。

我在CrudRepository上调用S save(S entity)方法进行保存。 我是否有可能延长这一点以附加所需的sql?

我知道我可以编写自己的自定义插入/更新语句,但是如果我可以附加到生成的sql那将是非常好的。

1 个答案:

答案 0 :(得分:0)

我设法通过扩展Hibernate的<Window x:Class="WPFttest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFttest" mc:Ignorable="d" Title="MainWindow" Height="362" Width="525" MouseDown="Window_MouseDown" BorderBrush="Red" ResizeMode="NoResize" KeyDown="Window_KeyDown" WindowStyle="None"> <Grid x:Name="mainGrid" HorizontalAlignment="Left" Height="362" VerticalAlignment="Top" Width="525" OpacityMask="#00000000"> <Canvas x:Name="drawArea" HorizontalAlignment="Left" Height="312" Margin="0,50,0,0" VerticalAlignment="Top" Width="525" Background="White" MouseDown="Canvas_MouseDown"> </Canvas> <Grid HorizontalAlignment="Left" Height="50" VerticalAlignment="Top" Width="525" MouseDown="Grid_MouseDown_1" OpacityMask="#00000000" Background="#00000000"/> </Grid> 并覆盖EmptyInterceptor方法来获得所需的结果。

String onPrepareStatement(String  sql)

我还必须在public class MyInterceptor extends EmptyInterceptor { @Override public String onPrepareStatement(String sql) { if (sql.startsWith("insert") || sql.startsWith("update")) { sql += " with none"; } return sql; } }

中指定此拦截器
application.properties

请注意,spring.jpa.properties.hibernate.ejb.interceptor=fully.qualified.name.MyInterceptor 实现了EmptyInterceptor接口,并且Interceptor方法已在接口中标记为已弃用。

建议使用onPrepareStatement接口,但我无法弄清楚如何使用Spring Boot设置它。所以,如果你这样做,请分享。

<强>更新

您可以在StatementInspector

中指定StatementInspector的实施方式
application.properties
相关问题