我目前面临一个让我发疯的问题。
我想创建自己的IdentifierGenerator来管理具有特定格式的ID。
public class CustomGenerator implements IdentifierGenerator {
private static final String REQUEST = "SELECT CURRVAL('sequence')";
private final String prefixFormat = "PREFIX-%s";
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
final String prefix = getPrefix();
Session sessionHibernate = (Session) session;
Query query = sessionHibernate.createSQLQuery(REQUEST );
Object obj = query.uniqueResult();
return null;
}
private String getPrefix() {
final Calendar date = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
return String.format(prefixFormat, dateFormat.format(date.getTime()));
}
}
我的Junit测试:
@Test
@Transactional
public void addEntity() {
final Entity entity= new Entity();
final long count = this.entityRepository.count();
this.entityRepository.saveAndFlush(rex);
assertEquals(count + 1, this.entityRepository.count());
}
我的实体:
@Entity
@Table(name = "ENTITY")
public class Entity {
@Id
@GenericGenerator(name = "sequence", strategy = "[...]CustomGenerator")
@GeneratedValue(generator = "sequence")
@Column(name = "ENTITY_ID")
private Integer entityId;
[...]
}
我的JUnit测试崩溃 - Stacktrace:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [SELECT CURRVAL('sequence_rex_id')]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
数据库:HSQLDB(测试),POSTGRESQL(生产)
有没有人有任何想法?
对不起我可能犯过的任何错误。英语不是我的母语。
答案 0 :(得分:0)
{
"Employee1": {
"Year1": {
"StartRange": 22,
"EndRange": 44
},
"Year2": {
"StartRange": 24,
"EndRange": 55
}
},
"Employee2": {
"Year1": {
"StartRange": 29,
"EndRange": 40
},
"Year2": {
"StartRange": 44,
"EndRange": 77
}
}
}
语法特定于PostgreSQL。在HSQLDB中使用PostgreSQL语法时,应在JDBC连接URL中包含必要的SQL语法标志。例如SELECT CURRVAL('sequence')