I have an error when making a query with hibernate in JSF in a log with relational tables

0

Errors that catch my attention but I do not know how to solve them

Initial SessionFactory creation failed.org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
11-Jun-2017 21:43:39.164 WARNING [http-nio-8084-exec-112] com.sun.faces.lifecycle.InvokeApplicationPhase.execute #{loginFormBean.searchUser()}: java.lang.ExceptionInInitializerError
javax.faces.FacesException: #{loginFormBean.searchUser()}: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)


Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:400)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at aplicacion.hibernate.config.HibernateUtil.<clinit>(HibernateUtil.java:24)
... 45 more

Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for agente in class aplicacion.modelo.dominio.CargoDeclarado
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:310)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:304)
at org.hibernate.mapping.Property.getGetter(Property.java:323)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:411)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:200)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:80)

Hibernate Useful

package aplicacion.hibernate.config;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
* Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Trabajo Y Estudio
 */
public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration().configure("/aplicacion/hibernate/config/hibernate.cfg.xml").buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

Bean

@ManagedBean
@ViewScoped
public class LoginBean implements Serializable {

private User user;
private UserDAO userDao;
public LoginBean() {
}

@PostConstruct
public void init() {
    userDao = new UserDAOImp();
}

public String searchUser(String username, String pass) {
    String word = null;
    user = userDao.searchUser(username, pass);
    if (user == null) {
        FacesContext.getCurrentInstance()
                .addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Atencion", "Uasuario No Encontrado"));
    } else {
        FacesContext.getCurrentInstance()
                .getExternalContext()
                .getSessionMap().put("user", user);
        word = "menu?faces-redirect=true";
    }
    return word;
}

public User sessionUser(){
    User user2 = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
    return user2;
}

/**
 * @return the user
 */
public User getUser() {
    return user;
}

/**
 * @param user the user to set
 */
public void setUser(User user) {
    this.user = user;
}

/**
 * @return the userDao
 */
public UserDAO getUserDao() {
    return userDao;
}

/**
 * @param userDao the userDao to set
 */
public void setUserDao(UserDAO userDao) {
    this.userDao = userDao;
}

}

xml created by mapping defect and hibernate class

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 11/06/2017 21:26:05 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class catalog="ddjj" name="aplicacion.modelo.dominio.User" optimistic-lock="version" table="users">
<id name="username" type="string">
  <column length="25" name="username"/>
  <generator class="assigned"/>
</id>
<property name="password" type="string">
  <column length="45" name="password" not-null="true"/>
</property>
<property name="name" type="string">
  <column length="45" name="name" not-null="true"/>
</property>
<property name="lastname" type="string">
  <column length="45" name="lastname" not-null="true"/>
</property>
<property name="sex" type="char">
  <column length="1" name="sex" not-null="true"/>
</property>
<property name="tel" type="string">
  <column length="45" name="tel"/>
</property>
<set fetch="select" inverse="true" lazy="true" name="agenteses" table="agentes">
  <key>
    <column length="25" name="usuario_username" not-null="true"/>
  </key>
  <one-to-many class="aplicacion.modelo.dominio.Agente"/>
</set>
</class>
</hibernate-mapping>

class created by default of hibernate

public class User  implements java.io.Serializable {


 private String username;
 private String password;
 private String name;
 private String lastname;
 private char sex;
 private String tel;
 private Set agenteses = new HashSet(0);

public User() {
}


public User(String username, String password, String name, String lastname, char sex) {
    this.username = username;
    this.password = password;
    this.name = name;
    this.lastname = lastname;
    this.sex = sex;
}
public User(String username, String password, String name, String lastname, char sex, String tel, Set agenteses) {
   this.username = username;
   this.password = password;
   this.name = name;
   this.lastname = lastname;
   this.sex = sex;
   this.tel = tel;
   this.agenteses = agenteses;
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
public String getLastname() {
    return this.lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}
public char getSex() {
    return this.sex;
}

public void setSex(char sex) {
    this.sex = sex;
}
public String getTel() {
    return this.tel;
}

public void setTel(String tel) {
    this.tel = tel;
}
public Set getAgenteses() {
    return this.agenteses;
}

public void setAgenteses(Set agenteses) {
    this.agenteses = agenteses;
}




}

called the query user search

public class UserDataSource implements Serializable {

public static void add(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
}

public static void upDate(User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
}

public static User searchUser(String username, String pass) {
    User user = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(User.class);
    criteria.add(Restrictions.like("username", username))
            .add(Restrictions.like("password", pass));
    System.out.println("Dentro de la busqueda");
    System.out.println(criteria.list().isEmpty());
    if (!criteria.list().isEmpty()) {
        System.out.println("Dentro del if");
        user = (User) criteria.list().get(0);
    }
    session.close();
    return user;
}

}
    
asked by Hernan Jesus Diaz Yarbi 12.06.2017 в 03:09
source

1 answer

0

At some point hibernate invokes EntityTuplizerFactory.constructTuplizer which tries to create an instance of aplicacion.modelo.dominio.CargoDeclarado , and in that process invokes the getters of the members of that class, including agente and can not find it. This is where the PropertyNotFoundException

You can solve it by adding the public method getAgente() in your class CargoDeclarado

Having solved that, rerun the code to determine if the rest of the exceptions were related.

    
answered by 12.06.2017 / 04:02
source