package org.javab.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.javab.Vehicle;
import org.javab.Twowheel;
import org.javab.Fourwheel;
public class Transport {
public static void main(String[] args) {
// TODO Auto-generated method stub
Twowheel Twowheel = new Twowheel();
Twowheel.setTwowheel("two");
Twowheel.setVehicleid(1);
Twowheel.setVehiclename("bike and cycle");
Fourwheel Fourwheel = new Fourwheel();
Fourwheel.setVehicleid(2);
Fourwheel.setFourwheel("four");
Fourwheel.setVehiclename("car and bus");
Vehicle Vehicle = new Vehicle();
Vehicle.setVehiclename("vehicle name");
Vehicle.setVehicleid(3);
@SuppressWarnings("deprecation")
SessionFactory sessionfactory =new Configuration().configure().buildSessionFactory();
Session session =sessionfactory.openSession();
session.beginTransaction();
session.save(Vehicle);
session.save(Fourwheel);
session.save(Twowheel);
session.getTransaction().commit();
session.close();
}
}
The model class i have are below..
package org.javab;
import javax.persistence.*;
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="vehicle d type",
discriminatorType=DiscriminatorType.STRING)
public class Vehicle {
@Id /*@GeneratedValue(strategy=GenerationType.AUTO,generator = "SEQ")
@SequenceGenerator(name = "SEQ", sequenceName = "VEHICLE_SEQ", initialValue = 1)*/
public int getVehicleid() {
return vehicleid;
}
public void setVehicleid(int vehicleid) {
this.vehicleid = vehicleid;
}
public String getVehiclename() {
return vehiclename;
}
public void setVehiclename(String vehiclename) {
this.vehiclename = vehiclename;
}
private int vehicleid;
private String vehiclename;
}
Class TwoWheel
package org.javab;
import javax.persistence.Entity;
@Entity
public class Twowheel extends Vehicle {
private String Twowheel;
public String getTwowheel() {
return Twowheel;
}
public void setTwowheel(String twowheel) {
Twowheel = twowheel;
}
}
Class FourWheel
package org.javab;
import javax.persistence.Entity;
@Entity
public class Fourwheel extends Vehicle{
private String fourwheel;
public String getFourwheel() {
return fourwheel;
}
public void setFourwheel(String fourwheel) {
this.fourwheel = fourwheel;
}
}
the Exception
which i have been getting is ...
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: insert into Vehicle (vehiclename, vehicle d type, vehicleid) values (?, 'Vehicle', ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: ORA-00917: missing comma
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy14.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2849)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3290)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:80)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:186)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.javab.hibernate.Transport.main(Transport.java:42)
Caused by: java.sql.SQLSyntaxErrorException: ORA-00917: missing comma
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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 16 more
hibernate.cfg.xml.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@sae.corpxxxxxxxxxxxe.com:1600/crmrtld</property>
<property name="connection.username">XXXXXXXXX</property>
<property name="connection.password">XXXXXXXXXXXXXXXXXr</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Name the annotated Entity -->
<mapping class="org.javab.Userdetails"></mapping>
<mapping class="org.javab.Vehicle"></mapping>
<mapping class="org.javab.Fourwheel"></mapping>
<mapping class="org.javab.Twowheel"></mapping>
</session-factory>
</hibernate-configuration>
it was working fine with the 10g dialect and everything,, please also help me with generating id..i cant generate id in oracle? now i just hardcoded them just to make it work..
DiscriminatorColumn
is supposed to be name of the column. And in your case the name is "vehicle d type"
. And it's not allowed to use space in name of column.
So use some reasonable name for DiscriminatorColumn
. Note that this column must exist in SQL table.
Also note that for the inherited classes you need to specify @DiscriminatorValue("class-specific-value")
so hibernate can distinguish which class it actually is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.