Java 休眠:MySQLSyntaxErrorException:“字段列表”中的未知列“____”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21871118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate : MySQLSyntaxErrorException: Unknown column ' ____ ' in 'field list'
提问by Pankaj VishwasRao
I am new to Hibernate.
我是 Hibernate 的新手。
I want to fetch all records from Transaction table,
我想从事务表中获取所有记录,
I got following error
我收到以下错误
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column transactio2_.deleted' in 'field list'
Though deleted
column in present in Table
虽然deleted
列在表中
Entity Class :
实体类:
@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 :
休眠事务Bean:
@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();
}
What goes wrong in my code ?
我的代码出了什么问题?
Edit : Here is create statement of DB Table
编辑:这是创建数据库表的语句
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$$
回答by Jubin Patel
you are using 0 or 1 in tinyint
column use below mapping it will works.
您tinyint
在映射下方的列使用中使用 0 或 1它将起作用。
@Column(name = "deleted")
private boolean deleted;
and one more change if you need to try,
you are using boolean value 0,1
so you can use tinyint(1)
, which is enought for 0,1.
如果您需要尝试,还有一个更改,您使用的是布尔值,0,1
因此您可以使用tinyint(1)
,这对于 0,1 来说已经足够了。
you can find more about hibernate mapping here
您可以在此处找到有关休眠映射的更多信息
回答by diobert
I encountered the same issue if I manually create JPA Entities. Just make sure you have @Columnannotation for each getter function.
如果我手动创建 JPA 实体,我遇到了同样的问题。只需确保每个 getter 函数都有@Column注释。
@Column(name = "deleted")
public int getDeleted()
{
return deleted;
}
回答by kiedysktos
I experienced similar issue recently. In my case it was missing database column
我最近遇到了类似的问题。就我而言,它缺少数据库列