如何在JPA 1.0环境中使用Hibernate 3.x关闭bean验证?
我用persistence.xml尝试了几件事:
<persistence-unit name="bbstats" transaction-type="RESOURCE_LOCAL">
<properties>
DB stuff
<property name="javax.persistence.validation.mode" value="none" />
<property name="hibernate.validator.autoregister_listeners" value="false" />
</properties>
<validation-mode>NONE</validation-mode>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
最后一个导致
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'validation-mode'. One of '{"http://java.sun.com/xml/ns/persistence":jta-data-source, "http://java.sun.com/xml/ns/persistence":non-jta-data-source, "http://java.sun.com/xml/ns/persistence":mapping-file, "http://java.sun.com/xml/ns/persistence":jar-file, "http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":properties}' is expected.
Run Code Online (Sandbox Code Playgroud)
但没有上述工作.谁能告诉我如何在JPA 1.0实现上做到这一点?
我有一个列表中的枚举(例如)
enum State
{
UP,
DOWN,
RETRY
};
Run Code Online (Sandbox Code Playgroud)
我的数据库中的列是enum类型.当我尝试通过在查询中设置参数来执行Hibernate查询时setParameter("keyword", State.RETRY);,我收到错误
参数值[RETRY]与预期类型[package.name.State(n/a)]不匹配
在我的域的Glassfish 4.1 server.log中.我正在使用Hibernate 4.3.6.
在寻找对Hibernate的源代码,我看到发生了错误,因为线958-960在org.hibernate.jpa.spi.BaseQueryImpl:
private static boolean isValidBindValue(Class expectedType, Object value, TemporalType temporalType) {
if ( expectedType.isInstance( value ) ) {
return true;
}
...
return false;
}
Run Code Online (Sandbox Code Playgroud)
isValidBindValue 返回false,因此我收到消息.
由于这一行,它打印出String相应的enum值:
String.format("Parameter value [%s] did not match expected type [%s (%s)]",
bind,
parameterType.getName(),
extractName( temporalType )
)
Run Code Online (Sandbox Code Playgroud)
该bind目的是通过调用隐式转换为字符串值toString的方法Object,它表示enum State.RETRY.
那我怎么能说服Hibernate这 …
我有一个使用 MySQL 原生枚举值的 MySQL 表:
CREATE TABLE users (
id int NOT NULL AUTO_INCREMENT,
status enum('PENDING', 'ACTIVE', 'INACTIVE') NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
我想通过 JPA 在 Spring Boot 中访问它,所以我将它建模为具有 enum 属性的实体。
public enum Status {
PENDING,
ACTIVE,
INACTIVE;
}
@Entity
@Table(name = "users")
public class User {
@Id
@Column
private int id;
@Column
@Enumerated(EnumType.STRING)
private Status status;
/* Getters, Setters,… */
}
Run Code Online (Sandbox Code Playgroud)
当我启动我的应用程序时,我遇到了ddl-auto属性设置为validate我预计模式验证错误的情况:
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [status] in table [users]; found [enum (Types#CHAR)], …
我的模型包含一个枚举,它由JPA映射到int我的mysql数据库中的一列.
mysql有一个本机enum类型,使用起来会更方便.
如何配置JPA以使用此类型?
@Entity
public class User extends Model {
public enum Role {
User,
Moderator,
Admin,
}
public Role role;
}
Run Code Online (Sandbox Code Playgroud)