我正在使用mysql数据库和Spring Data,在其中我定义了这样的列:
@Lob
@Column(length = 1000)
private String messageContent;
此列用于存储大字符串。它与mysql完美配合,但是对于单元测试,我改用H2数据库。看起来H2忽略了@Lob注释,我得到了:
Caused by: java.sql.SQLDataException: data exception: string data, right truncation; table: MESSAGE column: MESSAGE_CONTENT
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy189.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 147 common frames omitted
Caused by: org.hsqldb.HsqlException: data exception: string data, right truncation; table: MESSAGE column: MESSAGE_CONTENT
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Table.enforceTypeLimits(Unknown Source)
at org.hsqldb.Table.generateAndCheckData(Unknown Source)
at org.hsqldb.Table.insertSingleRow(Unknown Source)
at org.hsqldb.StatementDML.insertRowSet(Unknown Source)
at org.hsqldb.StatementInsert.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 156 common frames omitted
我无法更改列定义,H2仅用于测试用例。我正在考虑通过执行ALTER TABLE语句手动更改列类型,但是也许有更好的解决方法?
答案 0 :(得分:1)
我在这里通过评论报告解决方案。您试图在该字段中插入比SET FOREIGN_KEY_CHECKS=0;
注释中指定的长度更长的内容。调整注释或检查您的内容。
答案 1 :(得分:1)
如果您正在寻找跨数据库的一致性,并且也在寻找一种类似于Spring Data的方法来与Spring Data实体相关联的内容,那么为什么不给Spring Content JPA一眼呢?像Spring Data一样,它为您的内容需求提供了一种抽象的,简单的,自以为是的编程模型。您可以添加如下内容:-
pom.xml
<!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa</artifactId>
<version>0.4.0</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>0.4.0</version>
</dependency>
配置
@Configuration
@EnableJpaStores
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class ContentConfig {
@Value("/org/springframework/content/jpa/schema-drop-h2.sql")
private Resource dropReopsitoryTables;
@Value("/org/springframework/content/jpa/schema-h2.sql")
private Resource dataReopsitorySchema;
@Bean
DataSourceInitializer datasourceInitializer() {
ResourceDatabasePopulator databasePopulator =
new ResourceDatabasePopulator();
databasePopulator.addScript(dropReopsitoryTables);
databasePopulator.addScript(dataReopsitorySchema);
databasePopulator.setIgnoreFailedDrops(true);
DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource());
initializer.setDatabasePopulator(databasePopulator);
return initializer;
}
}
要关联内容,请将Spring Content注释添加到您的帐户实体。
Example.java
@Entity
public class Example {
// replace @Lob field with
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
// if you have rest endpoints
@MimeType
private String mimeType = "text/plain";
创建“商店”:
ExampleStore.java
@StoreRestResource(path="examplesContent")
public interface ExampleStore extends ContentStore<Example, String> {
}
这就是在/examplesContent
上创建REST端点所需的全部。当您的应用程序启动时,Spring Content将查看您的依赖项(请参阅Spring Content JPA / REST),查看您的ExampleStore
接口,并为JPA注入该接口的实现。它还将注入一个@Controller
来将http请求转发到该实现。这省去了您自己实施任何此类操作的麻烦。
所以...
curl -X POST /examplesContent/{exampleId}
会将内容存储在数据库中,并将其与ID为exampleId
的示例实体相关联。
curl /examplesContent/{exampleId}
将再次获取它,依此类推...支持完整的CRUD。
HTH