gpt4 book ai didi

java - 尽管在 Hibernate 映射文件中访问了 ="field",Hibernate 仍在寻找 setter

转载 作者:行者123 更新时间:2023-11-30 03:50:06 24 4
gpt4 key购买 nike

我收到以下错误:

PropertyNotFoundException: Could not find a setter for property firstName in class com.example.SpecialUser

尽管在我的 SpecialUser.hbm.xml 文件中将“default-access”和“access”设置为“field”:

<hibernate-mapping package="com.example" default-cascade="all" default-access="field">
<joined-subclass extends="User" name="com.example.SpecialUser" table="special_user" lazy="false">
<key column="user_id"/>
<property name="firstName" column="first_name" type="java.lang.String" access="field" />
</joined-subclass>
</hibernate-mapping>

SpecialUser 类:

public class SpecialUser extends User {

private static final long serialVersionUID = 101162430966683621L;

private String firstName;

public SpecialUser() {
super();
}

public String getFirstName() {
return this.firstName;
}

}

用户 hibernate 映射文件:

<class name="User" table="user" lazy="false">
<id name="userId" column="user_id" type="java.lang.String">
<generator class="assigned"></generator>
</id>

<property name="password" column="password" type="java.lang.String" />
</class>

最佳答案

如果您在较大的项目中遇到此问题,我建议您创建一个剥离项目,该项目仅具有持久化实体所需的基本配置并从那里开始。乍一看,您的配置唯一看起来奇怪的是 User 类没有包声明。

在所有测试中,我将 User 类移至 com.example 包中并修改 *.hbm.xml 文件以匹配该更改。

测试 1

  • Hibernate 3.6.10.Final
  • Spring 4.0.6.RELEASE [LocalSessionFactoryBean]
  • context.xml(如下)

结果:编译并运行简单的持久性测试。

测试 2

  • Hibernate 3.6.10.Final。
  • hibernate.cfg.xml(如下)

结果:编译并运行简单的持久性测试。

测试3

  • hibernate 3.3.1.GA。
  • hibernate.cfg.xml(如下)

结果:编译并运行简单的持久性测试。

您能否在项目之外执行类似的测试?如果您能够重现该错误,请提供重现该小测试所需的信息。

下面的完整代码用于比较。

<小时/>用户.java

package com.example;

import java.io.Serializable;

public class User implements Serializable {
private static final long serialVersionUID = -7240509760819729807L;

private String userId;
private String password;

public User() {
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

用户.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.example.User" table="user" lazy="false">
<id name="userId" column="user_id" type="java.lang.String">
<generator class="assigned"></generator>
</id>

<property name="password" column="password" type="java.lang.String" />
</class>
</hibernate-mapping>

SpecialUser.java

package com.example;

public class SpecialUser extends User {
private static final long serialVersionUID = 101162430966683621L;

private String firstName;

public SpecialUser() {
super();
}

public String getFirstName() {
return this.firstName;
}
}

SpecialUser.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.example" default-cascade="all" >
<joined-subclass extends="com.example.User" name="com.example.SpecialUser" table="special_user" lazy="false">
<key column="user_id" />
<property name="firstName" column="first_name" type="java.lang.String" access="field" />
</joined-subclass>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:testdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>

<mapping resource="model/User.hbm.xml" />
<mapping resource="model/SpecialUser.hbm.xml" />
</session-factory>
</hibernate-configuration>

上下文.xml

<?xml version="1.0" encoding="UTF-8"?>       
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">

<jdbc:embedded-database id="dataSource" />

<bean id="sessionFactory" primary="true" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>

<property name="mappingLocations">
<list>
<value>model/SpecialUser.hbm.xml</value>
<value>model/User.hbm.xml</value>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

关于java - 尽管在 Hibernate 映射文件中访问了 ="field",Hibernate 仍在寻找 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24674076/

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