Hibernate无法执行查询getInt()的无效值

时间:2014-11-06 22:52:17

标签: java hibernate java-ee

我有两个MYSQL表:帐户和用户。一个用户可以拥有多个帐户,因此它是一对多的关系。 帐户包含以下列:id,user,title,bank,type。它还有一个条目:

  

1 user1检查BankName借记

用户拥有以下列:id,first,last,username,password。它也有一个条目:

  

1 John Doe user1 hunter2

我为每个人制作了类和hibernate映射:

public class Account {

    private int id;
    private User user;
    private String title;
    private String bank;
    private String type;
    private double amount;

    public Account() {
    }

    public Account(User usr, String nm, String bnk, String typ) {
         this.user = usr;
        this.name = ttl;
        this.bank = bnk;
        this.type = typ;
    }
    //getters and setters
 }

这是我的映射

<class name="com.package.dao.beans.Account" table="budgeting.ACCOUNT">
    <meta attribute="class-description">
        This class contains the account detail.
    </meta>
    <id name="id" type="int" column="id">
        <generator class="native" />
    </id>
    <many-to-one name="user" class="com.package.dao.beans.User"
        column="user" unique="true" not-null="true"/>
    <property name="title" column="title" type="string" />
    <property name="bank" column="bank" type="string" />
    <property name="type" column="type" type="string" />

</class>

这是我的用户类

import java.util.Set;

public class User {
private int id;
private String first;
private String last;
private String username;
private String password;

public User() {
};

public User(String fName, String lName, String uName, String pass) {
    this.first = fName;
    this.last = lName;
    this.username = uName;
    this.password = pass;
};
//getters and setters
}

和帐户&#39;映射

    <class name="com.package.dao.beans.User" table="budgeting.users">
    <meta attribute="class-description">
        This class contains the account detail.
    </meta>
    <id name="id" type="int" column="id">
        <generator class="native" />
    </id>
    <property name="first" column="first" type="string" />
    <property name="last" column="last" type="string" />
    <property name="username" column="username" type="string" />
    <property name="password" column="password" type="string" />
</class>

当我运行一些代码打印出所有帐户(只有一个)时,

public class ManageAccount {
private static SessionFactory factory;

public static void main(String[] args) {
    try {
        factory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }

    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        List accounts = session.createQuery("FROM Account").list();
        for (Iterator iterator = accounts.iterator(); iterator.hasNext();) {
            Account acc = (Account) iterator.next();
            // System.out.print("First Name: " + acc.getUser().getFirst());
            System.out.print("  Last Name: " + acc.getName());
            System.out.println("  Salary: " + acc.getType());
        }
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}

我收到此错误

org.hibernate.exception.GenericJDBCException: could not execute query
Caused by: java.sql.SQLException: Invalid value for getInt() - 'user1'

我不明白为什么它会使用getInt(),它显然是一个字符串或类。

2 个答案:

答案 0 :(得分:1)

问题是由于以下映射

<many-to-one name="user" class="com.package.dao.beans.User"
        column="user" unique="true" not-null="true"/>

它实际上说的是,Account实体被映射为一个用户的关系的很多部分,使用Account表的列用户作为外键。 但是用户的主键是int

    <id name="id" type="int" column="id">
        <generator class="native" />
    </id>

虽然从您的样本数据中推断出的Account中的用户列显然是用户名(即字符串)。

您可以使用User id(int)作为Account表中的外键,而不是用户名

答案 1 :(得分:0)

我在查询中连接表时遇到了这个问题。 Hibernate无法正确地将列映射到正确的字段,因为我有重复的名称。在您的Java类中,它是可以的,但您应该确保每个列名都是唯一的,特别是如果您要连接表。这就是我要做的事情:

<class name="com.package.dao.beans.Account" table="budgeting.ACCOUNT">
    <meta attribute="class-description">
        This class contains the account detail.
    </meta>
    <id name="id" type="int" column="account_id"> // ***** change here
        <generator class="native" />
    </id>
    <many-to-one name="user" class="com.package.dao.beans.User"
        column="account_user_id" unique="true" not-null="true"/> // ***** change here
    <property name="title" column="title" type="string" />
    <property name="bank" column="bank" type="string" />
    <property name="type" column="type" type="string" />

</class>

<class name="com.package.dao.beans.User" table="budgeting.users">
    <meta attribute="class-description">
        This class contains the account detail.
    </meta>
    <id name="id" type="int" column="user_id">  // ***** change here
        <generator class="native" />
    </id>
    <property name="first" column="first" type="string" />
    <property name="last" column="last" type="string" />
    <property name="username" column="username" type="string" />
    <property name="password" column="password" type="string" />
</class>