java - 获取 hibernate 的 userId 的 PropertyAccessException

标签 java hibernate persistence hql

我已经做了一个在 hibernate 中使用数据库登录的示例。我的 Xml 和实体类是 用户.java:

import java.util.Date;
import javax.persistence.Entity;

@Entity(name="User")
public class User {

    private Integer userID;
    private String username;
    private String password;
    private Date createdDate;

    /**
     * Default Constructor
     */
    public User() {
        super();
    }

    public User(Integer userID, String username, String password) {
        this.userID = userID;
        this.username = username;
        this.password = password;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

}//end of class()

UserBooks.java:

public class UsersBooks {

    // Declaring all attributes
    private double userID;
    private String bookID;
    private String permission;

    // Constructors
    public UsersBooks() {
        super();
    }

    public UsersBooks(double userID, String bookID, String permission) {
        super();
        this.userID = userID;
        this.bookID = bookID;
        this.permission = permission;
    }

    // Getters and Setters
    public double getUserID() {
        return userID;
    }

    public void setUserID(double userID) {
        this.userID = userID;
    }

    public String getBookID() {
        return bookID;
    }

    public void setBookID(String bookID) {
        this.bookID = bookID;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    // Overriding toString()
    @Override
    public String toString() {
        return "UsersBooks [userID=" + userID + ", bookID=" + bookID
                + ", permission=" + permission + "]";
    }//end of toString()

}//end of class

UserSocialInfo.java:

public class UserSocialInfo {


    private int socialID;

    private String loginProvider;
    private String loginProviderUID;
    private String UID;
    private String nickname;
    private String photoURL;
    private String thumbnailURL;
    private int birthDay;
    private int birthMonth;
    private int birthYear;
    private String gender;
    private String proxiedEmail;
    private String country;
    private String state;
    private String city;
    private String zip;
    private String firstname;
    private String lastname;
    private String profileURL;
    private int userID;

    /**
     * 
     */
    public UserSocialInfo() {
        // TODO Auto-generated constructor stub
    }
    /**
     * @return the socialID
     */
    public int getSocialID() {
        return socialID;
    }

    /**
     * @param socialID the socialID to set
     */
    public void setSocialID(int socialID) {
        this.socialID = socialID;
    }

    /**
     * @return the loginProvider
     */
    public String getLoginProvider() {
        return loginProvider;
    }

    /**
     * @param loginProvider the loginProvider to set
     */
    public void setLoginProvider(String loginProvider) {
        this.loginProvider = loginProvider;
    }

    /**
     * @return the loginProviderUID
     */
    public String getLoginProviderUID() {
        return loginProviderUID;
    }

    /**
     * @param loginProviderUID the loginProviderUID to set
     */
    public void setLoginProviderUID(String loginProviderUID) {
        this.loginProviderUID = loginProviderUID;
    }

    /**
     * @return the uID
     */
    public String getUID() {
        return UID;
    }

    /**
     * @param uID the uID to set
     */
    public void setUID(String uID) {
        UID = uID;
    }

    /**
     * @return the nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * @param nickname the nickname to set
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * @return the photoURL
     */
    public String getPhotoURL() {
        return photoURL;
    }

    /**
     * @param photoURL the photoURL to set
     */
    public void setPhotoURL(String photoURL) {
        this.photoURL = photoURL;
    }

    /**
     * @return the thumbnailURL
     */
    public String getThumbnailURL() {
        return thumbnailURL;
    }

    /**
     * @param thumbnailURL the thumbnailURL to set
     */
    public void setThumbnailURL(String thumbnailURL) {
        this.thumbnailURL = thumbnailURL;
    }

    /**
     * @return the birthDay
     */
    public int getBirthDay() {
        return birthDay;
    }

    /**
     * @param birthDay the birthDay to set
     */
    public void setBirthDay(int birthDay) {
        this.birthDay = birthDay;
    }

    /**
     * @return the birthMonth
     */
    public int getBirthMonth() {
        return birthMonth;
    }

    /**
     * @param birthMonth the birthMonth to set
     */
    public void setBirthMonth(int birthMonth) {
        this.birthMonth = birthMonth;
    }

    /**
     * @return the birthYear
     */
    public int getBirthYear() {
        return birthYear;
    }

    /**
     * @param birthYear the birthYear to set
     */
    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the proxiedEmail
     */
    public String getProxiedEmail() {
        return proxiedEmail;
    }

    /**
     * @param proxiedEmail the proxiedEmail to set
     */
    public void setProxiedEmail(String proxiedEmail) {
        this.proxiedEmail = proxiedEmail;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * @return the state
     */
    public String getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(String state) {
        this.state = state;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the zip
     */
    public String getZip() {
        return zip;
    }

    /**
     * @param zip the zip to set
     */
    public void setZip(String zip) {
        this.zip = zip;
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the profileURL
     */
    public String getProfileURL() {
        return profileURL;
    }

    /**
     * @param profileURL the profileURL to set
     */
    public void setProfileURL(String profileURL) {
        this.profileURL = profileURL;
    }

    /**
     * @return the userID
     */
    public int getUserID() {
        return userID;
    }

    /**
     * @param userID the userID to set
     */
    public void setUserID(int userID) {
        this.userID = userID;
    }
}

我的 xml 文件 用户.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.entities.User" table="users">
        <id name="userID" type="long" column="userid">
            <generator class="increment" />
        </id>
        <property name="username" column="username" />
        <property name="password" column="password" />
        <property name="userID" column="userid" insert="false" update="false"/>
        <property name="createdDate" column="created_date" />
    </class>
</hibernate-mapping>

UserBooks.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.entities.UsersBooks" table="users_books">
        <id name="userID" type="long" column="user_id">
        </id>
        <property name="userID" column="user_id" insert="false" update="false"/>
        <property name="bookID" column="book_id" insert="false" update="false"/>
        <property name="permission" column="permissions" />
    </class>
</hibernate-mapping>

UserSocialInfo.java:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.entities.UserSocialInfo" table="user_social_info">
        <id name="socialID" type="long" column="social_id">
            <generator class="increment" />
        </id>
        <property name="socialID" column="social_id" insert="false" update="false"/>
        <property name="loginProvider" column="login_provider" />
        <property name="loginProviderUID" column="login_provider_uid" insert="false" update="false"/>
        <property name="UID" column="uid" insert="false" update="false"/>
        <property name="nickname" column="nickname" />
        <property name="photoURL" column="photo_url" />
        <property name="thumbnailURL" column="thumbnail_url" />
        <property name="birthDay" column="birthday" />
        <property name="birthMonth" column="birthmonth" />
        <property name="birthYear" column="birthyear" />
        <property name="gender" column="gender" />
        <property name="proxiedEmail" column="proxied_email" />
        <property name="country" column="country" />
        <property name="state" column="state" />
        <property name="city" column="city" />
        <property name="zip" column="zip" />
        <property name="firstname" column="firstname" />
        <property name="lastname" column="lastname" />
        <property name="profileURL" column="profile_url" />
        <property name="userID" column="user_id" insert="false" update="false"/>
    </class>
</hibernate-mapping>

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="fabulaFactory">
            <property name="connection.driver_class">
                 com.mysql.jdbc.Driver
            </property>
        <property name="connection.url">
             jdbc:mysql://localhost:3306/appname
        </property>
        <property name="connection.username">
             user
        </property>
        <property name="connection.password">
            pwd
        </property>
            <property name="connection.pool_size">5</property>
            <!-- SQL dialect -->
            <property name="dialect">
                 org.hibernate.dialect.MySQLInnoDBDialect
            </property>
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">false</property>
               <property name="hbm2ddl.auto">update</property>
        <mapping resource="com//hibernatexmlfiles//User.hbm.xml"/>
        <mapping resource="com//hibernatexmlfiles//UsersBooks.hbm.xml"/>
        <mapping resource="com//hibernatexmlfiles//UserSocialInfo.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

在执行项目时,我收到如下错误:

  org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.fabulait.fabula.entities.User.userID
        at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:70)
        at org.hibernate.tuple.AbstractTuplizer.setIdentifier(AbstractTuplizer.java:130)
        at org.hibernate.persister.entity.BasicEntityPersister.setIdentifier(BasicEntityPersister.java:2930)
        at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:146)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
        at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
        at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:477)
        at org.hibernate.impl.SessionImpl.save(SessionImpl.java:472)
        at com.service.UserService.storeSessionInfo(UserService.java:410)
        at com.web.LoginUser.doPost(LoginUser.java:122)
        at com.web.LoginUser.doGet(LoginUser.java:48)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:679)
    Caused by: java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
        ... 32 more

请大家给点建议。

最佳答案

应该是

from User u where u.username=:username

我假设User是与表映射的实体类(@Entity)

关于java - 获取 hibernate 的 userId 的 PropertyAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10913636/

相关文章:

xml - JPA 和 Tomcat 配置

java - 在实体中使用嵌入式类时传递的分离实体保持不变

java - 如何隐藏数据库密码?

java - jpa2 hibernate,一个测试实体锁定的测试用例

java - 如何让应用程序中的所有组件响应特定的按键事件?

java - 如何在JPA/Hibernate中根据两个外键生成id?

java - 来自子对象字段聚合的自定义 Hibernate POJO 字段

java - Bump API Android 崩溃

java - 如何更改 Netbeans 中的 JTable 标题单元格颜色

java - hibernate.properties 类路径冲突