facebook

JSF and Hibernate

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #249282 Reply

    David Larkin
    Member

    I’m trying to use jsf with hibernate to extend the login demo. What I want is the user to log in by checking the db table called USER_ACCOUNT.

    I’ve added hibernate capabilities to my login jsf project which created an abstract (AbstractUserAccount) and a concrete class (UserAccount) and mapped to the table.

    The problem comes when I try to use UserAccount or LoginBean as my backing bean. I keep getting:

    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Error reading resource: com/finalgrade/hibernate/UserAccount.hbm.xml
    ......
    Caused by: org.hibernate.MappingException: duplicate import: com.finalgrade.hibernate.UserAccount
    

    Basically what I’ve done is put getters/setters from my original LoginBean into the UserAccount class because everytime I instanciate UserAccount from LoginBean, I get the above error. And from what I’ve read, the error is due to multiple instances of the mapped UserAccount class.

    Which class should I be using for my backing bean, and how can I get jsf to work with hibernate? Will there be a tutorial for this in the future?

    Thanks in advance.

    #249283

    Haris Peco
    Member

    delta.echo,

    First, check your *cfg.xml files if you have com/finalgrade/hibernate/UserAccount.hbm.xml added twice
    If no, then it depend from your session configuration – hibernate have tried load this file second time

    Best

    #249284

    David Larkin
    Member

    I checked all the cfg.xml files and the only instance I found was in faces_cfg.xml where I make UserAccount a managed bean. Taking that out (making UserAccount “unmanaged”) gives me:

    PropertyNotFoundException: Error testing property 'username' in bean of type null

    maybe some code would be of use…

    package com.finalgrade.hibernate;
    
    import java.util.List;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    
    /**
     * UserAccount generated by MyEclipse - Hibernate Tools
     */
    public class UserAccount extends AbstractUserAccount implements java.io.Serializable {
    
        private String password;
        private String username;
        
        /**
         * @return Returns the password.
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * @param password The password to set.
         */
        public void setPassword(String password) {
            this.password = password;
        }
    
        /**
         * @return Returns the username.
         */
        public String getUsername() {
            return username;
        }
    
        /**
         * @param username The username to set.
         */
        public void setUsername(String username) {
            this.username = username;
        }
        
        // Constructors
    
        /** default constructor */
        public UserAccount() {
        }
        
        public UserAccount(String userName, String userPassword){
            super(userName, userPassword);
        }
        
        /** full constructor */
        public UserAccount(String userType, String userFirstName, String userLastName, String userEmail, String userPassword, byte userLoggedIn, Integer userSuccessLogon, byte userChgPass, String userName) {
            super(userType, userFirstName, userLastName, userEmail, userPassword, userLoggedIn, userSuccessLogon, userChgPass, userName);        
        }
       
        Session session = SessionManager.currentSession();
        
        
        public String hasAccess(){
            String gainAccess = "failure";
            
            String sqlQuery = "select `USER_TYPE` from `user_account` where USER_NAME = '" + getUsername() + "' and USER_PASSWORD = '" + getPassword() + "'";
            Query query = session.createQuery(sqlQuery);
            List list = query.list();
            if(list.isEmpty()){
                gainAccess = "success";
            }
            session.close();
            return gainAccess;
        }
        
    }
    
    #249290

    Haris Peco
    Member

    delta.echo ,

    How you make sessionFactory ?

    Best

    #249293

    David Larkin
    Member

    I followed the MyEclipse Hibernate QuickStart tutorial where SessionFactory was created with the wizard. I didn’t change the location of the config file, so that stayed the same. I then created the hibernate mapping to the table USER_ACCOUNT through the Database Explorer.

    #249297

    Haris Peco
    Member

    Please, can you send hibernate.cfg.xml

    Thanks

    #249304

    David Larkin
    Member

    Sure. As I mentioned, I haven’t changed the location from where the wizard placed it.

    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">
    
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
      <session-factory>
        <property name="connection.username">root</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/finalgrade</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="myeclipse.connection.profile">mySQL</property>
        <property name="connection.password">somefun</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <mapping resource="com/finalgrade/hibernate/UserAccount.hbm.xml"></mapping>
    
      </session-factory>
    
    </hibernate-configuration>
    
    
    #249309

    Haris Peco
    Member

    You have to check if hibernate grab *cfg.xml from other places – try debug configuration procedure

    #249312

    David Larkin
    Member

    Ok, I did a search in Help, but can’t find anything on what those procedures are.

    #249313

    Haris Peco
    Member

    delta.echo,

    You have made SessionManager (you choose other name maybe) on page 3 ‘Add hibernate’ wizard
    This is simple test for hibernate setting.Please, change to your session manager and try it

    public class HibernateTest {
    
        public static void main(String[] args) {
            Session session = SessionManager.currentSession();
            UserAccount account = new UserAccount();
            account.setUsername("test");
            account.setPassword("test");
            Transaction tx = session.beginTransaction();
            session.save(account);
            tx.commit();
            session.close();
            session = SessionManager.currentSession();
            account = (UserAccount) session.load(UserAccount.class,"test");
            tx = session.beginTransaction();
            session.delete(account);
            tx.commit();
            session.close();
        }
    
    }
    

    I suppose that you haven’t not-null columns in USER_ACCOUNT except USERNAME (if you have then set some value) and that username ‘test’ doesn’t exists

    Test add and delete username ‘test’ in table.If test success, your hibernate configuration is correct and we can
    explore problems in jsf

    Best regards

    #249322

    David Larkin
    Member

    I will do that. In the mean time, I wanted to show you some log output. Maybe this will help.

    
    Mar 25, 2006 2:37:35 PM org.hibernate.cfg.Environment <clinit>
    INFO: Hibernate 3.0.5
    Mar 25, 2006 2:37:35 PM org.hibernate.cfg.Environment <clinit>
    INFO: hibernate.properties not found
    Mar 25, 2006 2:37:35 PM org.hibernate.cfg.Environment <clinit>
    INFO: using CGLIB reflection optimizer
    Mar 25, 2006 2:37:35 PM org.hibernate.cfg.Environment <clinit>
    INFO: using JDK 1.4 java.sql.Timestamp handling
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration addResource
    INFO: Mapping resource: com/jsfhib/hibernate/UserAccount.hbm.xml
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
    INFO: Mapping class: com.jsfhib.hibernate.UserAccount -> user_account
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration doConfigure
    INFO: Configured SessionFactory: null
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration secondPassCompile
    INFO: processing extends queue
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration secondPassCompile
    INFO: processing collection mappings
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration secondPassCompile
    INFO: processing association property references
    Mar 25, 2006 2:37:36 PM org.hibernate.cfg.Configuration secondPassCompile
    INFO: processing foreign key constraints
    Mar 25, 2006 2:37:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Using Hibernate built-in connection pool (not for production use!)
    Mar 25, 2006 2:37:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Hibernate connection pool size: 20
    Mar 25, 2006 2:37:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: autocommit mode: false
    Mar 25, 2006 2:37:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/finalgrade
    Mar 25, 2006 2:37:37 PM org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: connection properties: {user=root, password=****}
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: RDBMS: MySQL, version: 5.0.18-nt
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.12 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
    Mar 25, 2006 2:37:38 PM org.hibernate.dialect.Dialect <init>
    INFO: Using dialect: org.hibernate.dialect.MySQLDialect
    Mar 25, 2006 2:37:38 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
    INFO: Using default transaction strategy (direct JDBC transactions)
    Mar 25, 2006 2:37:38 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
    INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Automatic flush during beforeCompletion(): disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Automatic session close at end of transaction: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC batch size: 15
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC batch updates for versioned data: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Scrollable result sets: enabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC3 getGeneratedKeys(): enabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Connection release mode: null
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Maximum outer join fetch depth: 2
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Default batch fetch size: 1
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Generate SQL with comments: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Order SQL updates by primary key: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
    INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    Mar 25, 2006 2:37:38 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
    INFO: Using ASTQueryTranslatorFactory
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Query language substitutions: {}
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Second-level cache: enabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Query cache: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory createCacheProvider
    INFO: Cache provider: org.hibernate.cache.EhCacheProvider
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Optimize cache for minimal puts: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Structured second-level cache entries: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Statistics: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Deleted entity synthetic identifier rollback: disabled
    Mar 25, 2006 2:37:38 PM org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Default entity-mode: pojo
    Mar 25, 2006 2:37:39 PM org.hibernate.impl.SessionFactoryImpl <init>
    INFO: building session factory
    Mar 25, 2006 2:37:39 PM net.sf.ehcache.config.Configurator configure
    WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: zip:C:/bea/user_projects/domains/mydomain/myserver/.wlnotdelete/extract/myserver__appsdir_jsfhib_dir_jsfhib/jarfiles/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
    Mar 25, 2006 2:37:40 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:40 PM org.hibernate.cfg.Configuration addResource
    INFO: Mapping resource: com/jsfhib/hibernate/UserAccount.hbm.xml
    Mar 25, 2006 2:37:40 PM org.hibernate.cfg.Configuration add
    SEVERE: Could not compile the mapping document
    org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        ...
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Error reading resource: com/jsfhib/hibernate/UserAccount.hbm.xml
        
    Caused by: org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        ... 77 more
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration addResource
    INFO: Mapping resource: com/jsfhib/hibernate/UserAccount.hbm.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration add
    SEVERE: Could not compile the mapping document
    org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        ...
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Error reading resource: com/jsfhib/hibernate/UserAccount.hbm.xml
        ...
    Caused by: org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        at org.hibernate.cfg.Mappings.addImport(Mappings.java:105)
        at org.hibernate.cfg.HbmBinder.bindPersistentClassCommonValues(HbmBinder.java:541)
        at org.hibernate.cfg.HbmBinder.bindClass(HbmBinder.java:488)
        at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:234)
        at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152)
        at org.hibernate.cfg.Configuration.add(Configuration.java:362)
        at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400)
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
        ... 90 more
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration addResource
    INFO: Mapping resource: com/jsfhib/hibernate/UserAccount.hbm.xml
    Mar 25, 2006 2:37:41 PM org.hibernate.cfg.Configuration add
    SEVERE: Could not compile the mapping document
    org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        at org.hibernate.cfg.Mappings.addImport(Mappings.java:105)
        ...
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Error reading resource: com/jsfhib/hibernate/UserAccount.hbm.xml
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:452)
        ...
    Caused by: org.hibernate.MappingException: duplicate import: com.jsfhib.hibernate.UserAccount
        at org.hibernate.cfg.Mappings.addImport(Mappings.java:105)
        ... 75 more
    Mar 25, 2006 2:37:41 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
    INFO: Not binding factory to JNDI, no JNDI name configured
    Mar 25, 2006 2:37:41 PM org.hibernate.impl.SessionFactoryImpl checkNamedQueries
    INFO: Checking 0 named queries
    Mar 25, 2006 2:37:42 PM com.sun.faces.renderkit.html_basic.HtmlBasicRenderer getForComponent
    
    #249323

    Haris Peco
    Member

    delta.echo,

    It’s clear from your first log message – you have duplicate import
    I think that your hibernate session grab another cf.xml from anywhere
    I can’t find from where, except you send complete project, but you can
    check your session manager and possible another hibernate.cfg.xml in your classpath

    Best

    #249332

    Brian Fernandes
    Moderator

    Also might be worth checking if you have a hibernate.properties file in your classpath.

    Brian.

    #249400

    David Larkin
    Member

    Brian – I checked the classpath and don’t see a dup hibernate.properties file.

    snpe – as this is a web project (jsf), how should I run the main method you suggested?

    Thanks.

    #249402

    David Larkin
    Member

    I’ve been trying to re-create my steps by rebuilding the project (double check myself). One thing that sticks out is when I try and map to the user_account table, I got a warning box stating I’m trying to select a hibernate 2 project. I specifically selected Hibernate 3 when adding hibernate support through MyEclipse. I let me proceed but warns of mapping errors.

    Could this be a problem?

Viewing 15 posts - 1 through 15 (of 20 total)
Reply To: JSF and Hibernate

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