facebook

Hi Newbie Having Trouble Writing TO DB with hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #268665 Reply

    ziggymon
    Member

    Hi, I’m more or less brand new to hibernate,
    I’ve created all the Mappings and Stuff
    I have A file called EmpDetails, Which reads from the DB No probs

    I’ve then created a write to java file thats suppossed to write to the DB which won’t work

    Here is my code And teh Log4j Report, Please Help !! I’ve been trying to implement it as part of a College Project

    Code

    package com.movies.hibernate;

    import java.sql.Date;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;

    import com.movies.hibernate.EmployeeDAO;
    import com.movies.hibernate.Employee;

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

    //Session session = HibernateSessionFactory.getSession();

    // Step 1 – Create the DAO
    EmployeeDAO dao = new EmployeeDAO();
    Employee Employee = new Employee();

    Employee.setEFname(“Pamela “);
    Employee.setESname(“O’Keeffe”);
    Employee.setEAdd1(“Avondale”);
    Employee.setEAdd2(“Waterford”);
    String dbirth = “25/10/1976”;
    SimpleDateFormat formater2 = new SimpleDateFormat(“dd/mm/yyyy”);
    java.util.Date parsedDate = null;
    try {
    parsedDate = formater2.parse(dbirth);
    } catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    java.sql.Date result = new java.sql.Date(parsedDate.getTime());
    Employee.setEDob(result);
    //Employee.setEPhone(051234235);
    //Employee.setEMobile(051234235);
    //System.out.println( “Employee Email ” + employee.getEEmail());
    // System.out.println( “Employee Role ” + employee.getRoll());

    Transaction tx = null;
    try {
    // Step 4 – Get a Hibernate Session
    Session session = dao.getSession();
    tx = session.beginTransaction();
    // Step 5 – Save the entity
    session.save(Employee);
    //tx.commit();
    System.out.println(“Save successful.”);
    } catch (HibernateException e) {
    tx.rollback();
    System.out.println(“Save failed.”);
    e.printStackTrace();
    } finally {
    try {
    dao.getSession().close();
    } catch (Exception e) {
    System.out.println(“Close failed.”);
    e.printStackTrace();
    }
    }
    }

    }

    Log 4j REport

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1287)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1309)
    at com.movies.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:59)
    at com.movies.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:43)
    at com.movies.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at com.movies.hibernate.EmpWriteTo.main(EmpWriteTo.java:48)
    %%%% Error Creating SessionFactory %%%%
    Close failed.
    org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1287)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1309)
    at com.movies.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:59)
    at com.movies.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:43)
    at com.movies.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at com.movies.hibernate.EmpWriteTo.main(EmpWriteTo.java:60)
    java.lang.NullPointerException
    at com.movies.hibernate.EmpWriteTo.main(EmpWriteTo.java:60)
    Exception in thread “main” java.lang.NullPointerException
    at com.movies.hibernate.EmpWriteTo.main(EmpWriteTo.java:49)

    #268667 Reply

    ziggymon
    Member

    Ok, I realised the Problem Here, I had Moved teh Hibernate /Config file, from its original position,
    So now it cann find teh COnfig file, Its runs ok, and the COnsole Reports that the Save was Successful
    Yet when I Lok at the DB through teh Oracle Interface there is no new entry

    #268672 Reply

    Haris Peco
    Member

    Uncomment this line :
    //tx.commit();

    Regards,

    #268680 Reply

    ziggymon
    Member

    Wow !! I’m some Idiot !! I’ve been going round in circles first, trying to convert a struts form input text to an sql date for writing
    and when that wouldn’t work, I’ve been panicking and just tried writing to the DB from java files, Never even saw that !!
    Thanks a Million !! I went to bed last night depressed, and today I feel a lot Better !!

    #268681 Reply

    ziggymon
    Member

    Could you tell Me Why are My Dates off ??

    I’ve Hard Coded a date to weite ti teh DB, yet whatever Month I pick it is recorded in the DB as January:

    Employee.setEFname(“Mary “);
    Employee.setESname(“O’Keeffe”);
    Employee.setEAdd1(“Avondale”);
    Employee.setEAdd2(“Waterford”);
    String dbirth = “10/03/73”;
    SimpleDateFormat formater2 = new SimpleDateFormat(“dd/mm/yyyy”);
    java.util.Date parsedDate = null;
    try {
    parsedDate = formater2.parse(dbirth);
    } catch (ParseException e1) {

    e1.printStackTrace();
    }
    java.sql.Date result = new java.sql.Date(parsedDate.getTime());
    Employee.setEDob(result);

    The DB REcords :

    2354234132 Mary O’Keeffe Avondale Waterford 10-JAN-71 – – – – –
    Edit 2354234133 Mary O’Keeffe Avondale Waterford 10-JAN-71 – – – – –
    Edit 2354234134 Mary O’Keeffe Avondale Waterford 10-JAN-73 – – – – –
    Edit 2354234135 Mary O’Keeffe Avondale Waterford 10-JAN-73

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Hi Newbie Having Trouble Writing TO DB with hibernate

You must be logged in to post in the forum log in