G
G
Gleb Tarasevich2018-08-14 22:32:53
Exceptions
Gleb Tarasevich, 2018-08-14 22:32:53

Throws an error org.hibernate.MappingException: Unknown entity what to do?

Please tell me, for what day I have been puzzling over the problem
Throws an error org.hibernate.MappingException: Unknown entity
Here is the Connection class

package com.DotClothes.connection;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class Connection {

    private static SessionFactory sessionFactory;

    private static SessionFactory configurationSessionFactory() {
        if(sessionFactory == null) {
            return new Configuration().configure().buildSessionFactory();
        }
        return null;
    }

    public static Session getSession() {
        return configurationSessionFactory().openSession();
    }
}

Address class
package com.DotClothes.model;

import lombok.Data;

import javax.persistence.*;

@Entity
@Data
@Table(name = "Address")
public class Address extends Model {


    @Column(name = "country")
    private String country;
    @Column(name = "city")
    private String city;
    @Column(name = "street")
    private String street;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_Customer", referencedColumnName = "Id_customer")
    private Customer customer;

    public Address(){
      super();
    }

    public Address(Long id){
        super(id);
    }

    public Address(String country, String city, String street) {
        this.country = country;
        this.city = city;
        this.street = street;
    }
}

AbstractDao class
package com.DotClothes.imp;

import com.DotClothes.connection.Connection;
import com.DotClothes.model.Model;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.lang.reflect.ParameterizedType;
import java.util.List;

public abstract class AbstractDao<T extends Model> {

    private Session session = Connection.getSession();
    private Class<T> clazz;

    @SuppressWarnings("unchecked")
    public AbstractDao() {
        final ParameterizedType supperClass = (ParameterizedType) getClass().getGenericSuperclass();
        this.clazz = (Class<T>) ((ParameterizedType) supperClass).getActualTypeArguments()[0];
    }

    public void add(T model) {
        try {
            session.beginTransaction();
            session.save(model);
            session.getTransaction().commit();
        }catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            session.flush();
            session.close();
        }
    }

    public void update(T model) {
        try {
            session.beginTransaction();
            session.merge(model);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.getMessage();
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
    }

    @SuppressWarnings("unchecked")
    public T get(long id) {
            Criteria getEntity = session.getSessionFactory().openSession().createCriteria(this.clazz);
            getEntity.add(Restrictions.eq("id", id));
            return (T) getEntity.uniqueResult();
    }

    public void remove(T model) {
        try {
            session.beginTransaction();
            session.delete(model);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.getMessage();
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<T> getAll() {
            Criteria getAllEntity = session.getSessionFactory().openSession().createCriteria(this.clazz);
            getAllEntity.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            return getAllEntity.list();
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question