• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}
}



 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic