gpt4 book ai didi

java - 将 Hibernate 与 H2 数据库和 joda DateTime 一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:21 25 4
gpt4 key购买 nike

我将 Hibernate 与 H2 内存数据库一起使用。数据库表是根据我在 persistence.xml 文件中提供的 bean 信息自动创建的。

H2 正在映射一个名为“created”的字段,类型为 org.joda.time.DateTimeVARBINARY(255)

类型的列

当我尝试使用值 created=DateTime.now() 持久化我的 bean 时,我收到以下错误:

    Caused by: org.h2.jdbc.JdbcSQLException: Value too long for column "CREATED BINARY(255)": "X'aced0005737200166f72672e6a6f64612e74696d652e4461746554696d65b83c78646a5bddf90200007872001f6f72672e6a6f64612e74696d652e62617365... (273)";  
SQL statement:
insert into mytable (id, created) values (null, ?) [22001-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)

知道为什么以及我可以做些什么来解决它吗?不幸的是,我无法更改 bean 定义,所以我必须继续使用 joda DateTime。

谢谢!

附言这是我的 persistence.xml

<persistence-unit name="my-test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<class>com.model.MyTable</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ALL</shared-cache-mode>

<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/c:\UnitTest;INIT=CREATE SCHEMA IF NOT EXISTS UnitTest" />
<property name="javax.persistence.jdbc.user" value="SA" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.show_sql" value = "true" />

<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.max_fetch_depth" value="4" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
</properties>

</persistence-unit>

以及Hibernate/H2自动生成的表:

MYTABLE
ID BIGINT(19) NOT NULL
CREATED VARBINARY(255)

这是我的 bean :

@Entity
@Table(name = "MyTable")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class MyTable implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

public Long getId() {
return id;
}

@SuppressWarnings("unused")
private void setId(Long id) {
this.id = id;
}

@Column(name = "created")
private DateTime created;

public DateTime getCreated() {
return created;
}

@SuppressWarnings("unused")
private void setCreated(DateTime created) {
this.created = created;
}

public MyTable() {
}

public MyTable(DateTime created) {
this.created = created;
}

@Override
public boolean equals(Object obj) {
...
}

@Override
public int hashCode() {
...
}
}

最佳答案

您应该使用 Joda-Time-Hibernate 映射:

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")

关于java - 将 Hibernate 与 H2 数据库和 joda DateTime 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22862183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com