• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to insert using Hql: getting error: unexpected token: from

 
nayar madhavan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OldStudent class:

package insert;

public class OldStudent {
private int id;
private String name;
private String email;

public OldStudent() {

}

public OldStudent(int id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

NewStudent class:

package insert;

public class NewStudent {
private int id;
private String name;
private String email;

public NewStudent() {

}

public NewStudent(int id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

oldstudent.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="insert.OldStudent" >
<id name="id" />
<property name="name" ></property>
<property name="email" ></property>
</class>

</hibernate-mapping>

newstudent.hbm.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="insert.NewStudent" >
<id name="id" />
<property name="name" ></property>
<property name="email" ></property>
</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="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.url">jdbcracle:thin:@localhost:1521:xe</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="insert/oldstudent.hbm.xml"/>
<mapping resource="insert/newstudent.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Client_insert class:

package insert;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class Client_Insert {
public static void main(String[] args) {

// creating configuration object

Configuration configuration = new Configuration();

// Populating the data of the configuration file

configuration.configure("insert/hibernate.cfg.xml");

// creating session factory object

StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());

SessionFactory sessionFactory = configuration.buildSessionFactory(standardServiceRegistryBuilder.build());

// creating session object

Session session = sessionFactory.openSession();

// creating a transaction object

Transaction transaction = session.beginTransaction();

// creating a student object
OldStudent oldStudent1 = new OldStudent(111, "stephen", "stephen@gmail.com");
OldStudent oldStudent2 = new OldStudent(112, "samson", "samson@gmail.com");
OldStudent oldStudent3 = new OldStudent(113, "alex", "alex@gmail.com");
OldStudent oldStudent4 = new OldStudent(114, "arnold", "arnold@yahoo.com");

session.save(oldStudent1);
session.save(oldStudent2);
session.save(oldStudent3);
session.save(oldStudent4);

transaction.commit();

session.close();
sessionFactory.close();

}
}

hql_table_table class


package insert;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HQL_Table_Table {

public static void main(String[] args) {

// creating configuration object

Configuration configuration = new Configuration();

// Populating the data of the configuration file

configuration.configure("insert/hibernate.cfg.xml");

// creating session factory object

StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());

SessionFactory sessionFactory = configuration.buildSessionFactory(standardServiceRegistryBuilder.build());

// creating session object

Session session = sessionFactory.openSession();

// creating a transaction object

Transaction transaction = session.beginTransaction();

Query query = session.createQuery(
"insert into NewStudent (id, name,email)" + " select o.id,o.name,o.email from OldStudent o");

int i = query.executeUpdate();
System.out.println("No of rows effected: " + i);
transaction.commit();
session.close();
sessionFactory.close();

}
}



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your question?

And please UseCodeTags when posting code.
 
nayar madhavan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting error in hql_table_table class when using the hql as below

Query query = session.createQuery(
"insert into NewStudent (id, name,email)" + " select o.id,o.name,o.email from OldStudent o");

and i am getting the following error
unexpected token: from
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic