我遇到了在数据库中插入数据的问题,其中STOCK_ID是标识,STOCK_NAME是字符串。在数据库中插入数据时,它显示了一个例外情况,如下所述。请指导我纠正这个问题。
Hibernate: insert into mkyongdb.system.STOCK (STOCK_NAME) values (?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.mkyong.user.Stock]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at com.mkyong.common.App.main(App.java:45)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00926: missing VALUES keyword
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1010)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1315)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57)
... 16 more
Stock.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.mkyong.user.Stock" table="STOCK" catalog="mkyongdb">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockName" type="string">
<column name="STOCK_NAME" length="10" />
</property>
<!-- <one-to-one name="stockDetail" class="com.mkyong.user.StockDetail"
cascade="save-update"></one-to-one> -->
</class>
</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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521/xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.default_schema">system</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping resource="com/mkyong/user/DBUser.hbm.xml"></mapping>
<mapping resource="com/mkyong/user/Stock.hbm.xml" />
<mapping resource="com/mkyong/user/StockDetails.hbm.xml" />
</session-factory>
</hibernate-configuration>
Stock.java
package com.mkyong.user;
public class Stock implements java.io.Serializable {
private Integer stockId;
private String stockName;
//private StockDetail stockDetail;
/*public StockDetail getStockDetail() {
return stockDetail;
}
public void setStockDetail(StockDetail stockDetail) {
this.stockDetail = stockDetail;
}*/
public Stock() {
//super();
// TODO Auto-generated constructor stub
}
public Stock(Integer stockId, String stockName) {
super();
this.stockId = stockId;
this.stockName = stockName;
}
public Integer getStockId() {
return stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
public String getStockName() {
return stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
}
Main.java
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Stock stock = new Stock();
stock.setStockName("sam");
session.save(stock);
session.getTransaction().commit();
创建查询:
CREATE TABLE STOCK
(
STOCK_ID numeric(10) not null,
STOCK_NAME varchar2(50) not null,
CONSTRAINT STOCK_PK PRIMARY KEY (STOCK_ID)
);
答案 0 :(得分:1)
mkyongdb
是您的架构,其中包含名为STOCK
的表。所以,
使用mkyongdb.STOCK
代替mkyongdb.system.STOCK
insert
声明。 STOCK_ID
是必填字段,在insert
声明中缺失。
(顺便说一句,如果您使用 Oracle12c ,insert
语句中可能不会包含此STOCK_ID numeric(10) GENERATED by default on null as IDENTITY
语句,前提是create table
DDL中定义为before insert
语句直接而不需要任何其他机制,如触发器。如果你有12c之前的版本,可能会创建一个:new.STOCK_ID := seq_stock_id.nextval;
触发器,其中包含seq_stock_id
语句,其中{{1}是一个序列)