Hibernate:MySQLSyntaxErrorException:'字段列表'中的未知列'____'

时间:2014-02-19 04:34:40

标签: java hibernate hibernate-criteria

我是Hibernate的新手。

我想从Transaction表中获取所有记录,

我收到了以下错误

 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column transactio2_.deleted' in 'field list'

虽然表中列出了deleted

实体类:

@Entity
@Table(name = "transaction")
public class Transaction implements Comparable<Transaction>
{
@Id
@GeneratedValue
@Column(name = "transactionId")
private int transactionId;

@Column(name = "date")
private Date date;

@Column(name = "amount")
private BigDecimal amount;

@ManyToOne
@JoinColumn(name = "transactionTypeId")
private TransactionType transactionType;

@ManyToOne
@JoinColumn(name = "userId")
private User user;

@Column(name = "operation")
private String operation;

@Column(name = "lastUpdate")
private Date lastUpdate;

@Column(name = "deleted")
private int deleted;

@Column(name = "remark")
private String remark;

public Transaction()
{
    super();
}

@Override
public int compareTo(Transaction c)
{
    if (getTransactionId() < c.getTransactionId())
    {
        return -1;
    }
    if (getTransactionId() > c.getTransactionId())
    {
        return 1;
    }
    return 0;
}

public Date getLastUpdate()
{
    return lastUpdate;
}

public void setLastUpdate(Date lastUpdate)
{
    this.lastUpdate = lastUpdate;
}

public User getUser()
{
    return user;
}

public void setUser(User user)
{
    this.user = user;
}

public int getDeleted()
{
    return deleted;
}

public void setDeleted(int deleted)
{
    this.deleted = deleted;
}

public int getTransactionId()
{
    return transactionId;
}

public void setTransactionId(int transactionId)
{
    this.transactionId = transactionId;
}

public Date getDate()
{
    return date;
}

public void setDate(Date date)
{
    this.date = date;
}

public BigDecimal getAmount()
{
    return amount;
}

public void setAmount(BigDecimal amount)
{
    this.amount = amount;
}

public TransactionType getTransactionType()
{
    return transactionType;
}

public void setTransactionType(TransactionType transactionType)
{
    this.transactionType = transactionType;
}

public String getOperation()
{
    return operation;
}

public String getRemark()
{
    return remark;
}

public void setRemark(String remark)
{
    this.remark = remark;
}

public void setOperation(String operation)
{
    this.operation = operation;
}
 }

Hibernate transactionBean:

@ManagedBean
@ViewScoped
public class TransactionBean implements Serializable
{

    ArrayList<Transaction> transactionList = new ArrayList<>();

    /**
   * Public Constructor
    */
public TransactionBean()
{
     Session session = HibernateUtil.getSessionFactory().openSession();
    @SuppressWarnings("unchecked")
    transactionList = (ArrayList<Transaction>) session.createCriteria(Transaction.class).list();     
}

我的代码出了什么问题?

编辑:这是DB表的创建声明

 delimiter $$

   CREATE TABLE `transaction` (
      `transactionId` int(11) NOT NULL AUTO_INCREMENT,
      `date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      `amount` decimal(10,2) DEFAULT NULL,
      `transactionTypeId` int(11) NOT NULL,
      `operation` varchar(45) DEFAULT NULL,
      `lastUpdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `userId` int(11) NOT NULL,
      `deleted` tinyint(4) NOT NULL DEFAULT '0',
      `remark` varchar(450) DEFAULT NULL,
        PRIMARY KEY (`transactionId`),
        KEY `fk_transaction_1` (`userId`),
        KEY `fk_transaction_2` (`transactionTypeId`),
        CONSTRAINT `fk_transaction_1` FOREIGN KEY (`userId`) REFERENCES `user` (`userId`) ON     DELETE NO ACTION ON UPDATE NO ACTION,
       CONSTRAINT `fk_transaction_2` FOREIGN KEY (`transactionTypeId`) REFERENCES   `transaction_type` (`transactionTypeId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1$$

3 个答案:

答案 0 :(得分:3)

如果我手动创建JPA实体,我遇到了同样的问题。 只需确保每个getter函数都有 @Column 注释。

@Column(name = "deleted")
public int getDeleted()
{
    return deleted;
}

答案 1 :(得分:0)

您在tinyint列中使用0或1使用下面的映射它将起作用。

@Column(name = "deleted")
private boolean deleted;

如果您需要尝试,还有一项更改, 你使用的是布尔值0,1所以你可以使用tinyint(1),这是0,1。

你可以找到更多关于hibernate mapping here

的信息

答案 2 :(得分:0)

我最近遇到过类似的问题。在我的情况下,它缺少数据库列