facebook

Hibernate operation: could not load an entity

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

    zhai1014
    Member

    hi,

    I use Myeclipse 4.1 and mysql and tomcat to make a little web project. I do the hibernate reverse enginering also. When I test methods in DAO class, I got some trouble.

    Here is the mapping file

    
    <hibernate-mapping>
        <class name="com.fly100.hibernate.Website" table="website">
            <id name="id" type="long" unsaved-value="0">
                <column name="id" />    
                <generator class="increment" />
            </id>
            <property name="webname" type="string">
                <column name="webname" length="100" />
            </property>
            <property name="weblink" type="string">
                <column name="weblink" length="100" />
            </property>
        </class>
    </hibernate-mapping>
    

    Here is part of Action class to run DAO class

    
            List l = websiteDao.findByExample(new Website());
            Iterator it = l.iterator();
            while (it.hasNext()) {
                Website webSite  = (Website)it.next();
                System.out.println(webSite.getWebname());
                System.out.println(webSite.getWeblink());
                
            }
            
            Website w = websiteDao.findById(new Long(1));
            websiteDao.save(new Website("a", "d"));
    

    The findByExample method is OK
    The findById method is wrong

    error msg followed.
    javax.servlet.ServletException: Hibernate operation: could not load an entity: [com.fly100.hibernate.Website#1]; uncategorized SQLException for SQL [select website0_.id as id0_, website0_.webname as webname0_0_, website0_.weblink as weblink0_0_ from website website0_ where website0_.id=?]; SQL state [S1000]; error code [0]; java.lang.ArrayIndexOutOfBoundsException: 6; nested exception is java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException: 6
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not load an entity: [com.fly100.hibernate.Website#1]; uncategorized SQLException for SQL [select website0_.id as id0_, website0_.webname as webname0_0_, website0_.weblink as weblink0_0_ from website website0_ where website0_.id=?]; SQL state [S1000]; error code [0]; java.lang.ArrayIndexOutOfBoundsException: 6; nested exception is java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException: 6
    org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:96)
    org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:257)
    org.springframework.orm.hibernate3.HibernateAccessor.convertJdbcAccessException(HibernateAccessor.java:424)
    org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:411)
    org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:370)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:464)
    org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:458)
    com.fly100.hibernate.WebsiteDAO.findById(WebsiteDAO.java:50)
    com.fly100.action.LoginAction.execute(LoginAction.java:61)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    save method is also wrong
    javax.servlet.ServletException: Hibernate operation: Could not execute JDBC batch update; uncategorized SQLException for SQL [insert into website (webname, weblink, id) values (?, ?, ?)]; SQL state [S1000]; error code [0]; java.lang.ArrayIndexOutOfBoundsException: 6; nested exception is java.sql.BatchUpdateException: java.lang.ArrayIndexOutOfBoundsException: 6
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Could not execute JDBC batch update; uncategorized SQLException for SQL [insert into website (webname, weblink, id) values (?, ?, ?)]; SQL state [S1000]; error code [0]; java.lang.ArrayIndexOutOfBoundsException: 6; nested exception is java.sql.BatchUpdateException: java.lang.ArrayIndexOutOfBoundsException: 6
    org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:96)
    org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:257)
    org.springframework.orm.hibernate3.HibernateAccessor.convertJdbcAccessException(HibernateAccessor.java:424)
    org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:411)
    org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:370)
    org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:687)
    com.fly100.hibernate.WebsiteDAO.save(WebsiteDAO.java:27)
    com.fly100.action.LoginAction.execute(LoginAction.java:62)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    anyone know it, please help me.

    #254293 Reply

    Haris Peco
    Member

    zhai1014 ,

    Please, can you send POJo and DAO classes

    Thanks

    #254355 Reply

    zhai1014
    Member

    POJO

    
     package com.fly100.hibernate;
    
    
    
    /**
     * Website generated by MyEclipse - Hibernate Tools
     */
    
    public class Website  implements java.io.Serializable {
    
    
        // Fields    
    
         private Long id;
         private String webname;
         private String weblink;
    
    
        // Constructors
    
        /** default constructor */
        public Website() {
        }
    
        
        /** full constructor */
        public Website(String webname, String weblink) {
            this.webname = webname;
            this.weblink = weblink;
        }
    
       
        // Property accessors
    
        public Long getId() {
            return this.id;
        }
        
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getWebname() {
            return this.webname;
        }
        
        public void setWebname(String webname) {
            this.webname = webname;
        }
    
        public String getWeblink() {
            return this.weblink;
        }
        
        public void setWeblink(String weblink) {
            this.weblink = weblink;
        }
       
    
    
    
    
    
    
    
    
    }
    

    DAO

    
    package com.fly100.hibernate;
    
    import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.criterion.Example;
    import org.springframework.context.ApplicationContext;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    /**
     * Data access object (DAO) for domain model class Website.
     * @see com.fly100.hibernate.Website
     * @author MyEclipse - Hibernate Tools
     */
    public class WebsiteDAO extends HibernateDaoSupport {
    
        private static final Log log = LogFactory.getLog(WebsiteDAO.class);
    
        protected void initDao() {
            //do nothing
        }
        
        public void save(Website transientInstance) {
            log.debug("saving Website instance");
            try {
                getHibernateTemplate().saveOrUpdate(transientInstance);
                log.debug("save successful");
            } catch (RuntimeException re) {
                log.error("save failed", re);
                throw re;
            }
        }
        
        public void delete(Website persistentInstance) {
            log.debug("deleting Website instance");
            try {
                getHibernateTemplate().delete(persistentInstance);
                log.debug("delete successful");
            } catch (RuntimeException re) {
                log.error("delete failed", re);
                throw re;
            }
        }
        
        public Website findById( java.lang.Long id) {
            log.debug("getting Website instance with id: " + id);
            try {
                Website instance = (Website) getHibernateTemplate()
                        .get("com.fly100.hibernate.Website", id);
                return instance;
            } catch (RuntimeException re) {
                log.error("get failed", re);
                throw re;
            }
        }
        
        
        public List findByExample(Website instance) {
            log.debug("finding Website instance by example");
            try {
                List results = getSession()
                        .createCriteria("com.fly100.hibernate.Website")
                        .add(Example.create(instance))
                .list();
                log.debug("find by example successful, result size: " + results.size());
                return results;
            } catch (RuntimeException re) {
                log.error("find by example failed", re);
                throw re;
            }
        }    
        
        public Website merge(Website detachedInstance) {
            log.debug("merging Website instance");
            try {
                Website result = (Website) getHibernateTemplate()
                        .merge(detachedInstance);
                log.debug("merge successful");
                return result;
            } catch (RuntimeException re) {
                log.error("merge failed", re);
                throw re;
            }
        }
    
        public void attachDirty(Website instance) {
            log.debug("attaching dirty Website instance");
            try {
                getHibernateTemplate().saveOrUpdate(instance);
                log.debug("attach successful");
            } catch (RuntimeException re) {
                log.error("attach failed", re);
                throw re;
            }
        }
        
        public void attachClean(Website instance) {
            log.debug("attaching clean Website instance");
            try {
                getHibernateTemplate().lock(instance, LockMode.NONE);
                log.debug("attach successful");
            } catch (RuntimeException re) {
                log.error("attach failed", re);
                throw re;
            }
        }
    
        public static WebsiteDAO getFromApplicationContext(ApplicationContext ctx) {
            return (WebsiteDAO) ctx.getBean("WebsiteDAO");
        }
    } 
    
    #254365 Reply

    Haris Peco
    Member

    zhai1014,

    I have tried reproduce your case and i can’t.
    Please can you try next :
    – call findById before findByExample
    – try do in simple test or java application (this is my example for your case)

    
    package com.fly100.hibernate;
    
    import java.util.Iterator;
    import java.util.List;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestSpringDAO {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    "/applicationContext.xml");
            WebsiteDAO websiteDAO = WebsiteDAO.getFromApplicationContext(context);
            List l = websiteDAO.findByExample(new Website());
            Iterator it = l.iterator();
            while (it.hasNext()) {
                Website webSite = (Website) it.next();
                System.out.println(webSite.getWebname());
                System.out.println(webSite.getWeblink());
    
            }
    
            Website w = websiteDAO.findById(new Long(1));
            websiteDAO.save(new Website("a", "d"));
        }
    }
    

    I have copied your case, make table (in mysql 5.0, jdbc 3.1.12) and it works.
    ArrayIndexOutOfBoundsException is strange error for findById and it’s possible that you have
    some jdbc driver problem

    Best regards

    PS
    Please, can you upgrade to newer version MyEclipse (version 4.1.1 have some fix for hibernate and version 5.0M2 have enhancements in generating DAO)
    What is your jdbc driver and version mysql ?

    #254493 Reply

    zhai1014
    Member

    Thank you very much!
    I change mysql jdbc jar, it works now.

    But I find one problem, when generating the hibernate mapping file, there is the catalog attribute, it lead to the sql error. But when you delete it, it is running OK. maybe that is the bug of hibernate reverse engineering.

    #254499 Reply

    Haris Peco
    Member

    zhai1014,

    It’s bug in hibernate 3.0.5 – we have fixed in 4.1.1 (i suppose) and you can have older hibernate files or you use older version MyEclipse

    I suggest that you upgrade to 5.0 M2 – there are a lot enhancements, but if you can’t (5.0 request eclipse 3.2) then upgrade to 4.1.1

    Best regards

    #256453 Reply

    ddumitru
    Member

    It may also be a bug in Hibernate one-to-one relationships. See http://opensource.atlassian.com/projects/hibernate/browse/HHH-1004#action_23791.

    The recommended “work around”/fix is to add “cascade=merge” to the one-to-one side of the mapping file.

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Hibernate operation: could not load an entity

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