Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import org.hibernate.Hibernate;
- import org.hibernate.Session;
- import org.hibernate.*;
- import org.hibernate.cfg.*;
- //import org.hibernate.classic.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import java.util.*;
- //import com.someorg.persist.Order;
- // use as
- // java test. FindOrderById name
- public class FindOrderById {
- public static void main(String[] args) throws Exception {
- String id1 ="id1";
- // query to issue
- /* String query =
- "select order from Order "
- + "where order.id=: id1";
- */
- // search for what?
- String name = args[0];
- // init
- Configuration cfg = new Configuration()
- .addClass(Order.class);
- cfg.setProperty("connection.driver_class","com.mysql.jdbc.Driver");
- cfg.setProperty("connection.url","jdbc:mysql://localhost:3306/hibernate");
- cfg.setProperty("connection.username","sandbox");
- cfg.setProperty("connection.password","sandbox");
- cfg.setProperty("connection.pool_size", 8 );
- cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect" );
- SessionFactory sf = cfg.buildSessionFactory();
- // sf.getDialect();
- //org.hibernate.dialect.Dialect.setDialect("org.hibernate.dialect.MySQLDialect");
- // open session
- Session sess = sf.openSession();
- Transaction tx = sess.beginTransaction();
- Query query = sess.createQuery("select o from Order as o where o.id = :id1");
- query.setString("id1", "id1");
- for (Iterator it = query.iterate(); it.hasNext();) {
- Order order = (Order) it.next();
- System.out.println("Order Price : " + order.getPriceTotal() );
- }
- tx.commit();
- // search and return
- sess.close();
- // System.out.println("Found Order: " + o);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement