Home > Net >  Hibernate - Could not resolve root entity
Hibernate - Could not resolve root entity

Time:01-07

I'm getting a "Could not resolve root entity 'PlayerStats'" UnknownEntityException while try to load a entity with spezific uuid. Inside the Test-Section it works fine but in when I compile it and try to run I received the error.

PlayerStats

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;

@Getter
@Setter
@NoArgsConstructor
@Table
@Entity
public class PlayerStats implements Serializable {

    public PlayerStats(String UUID, int deaths, int placed, int destroyed, String settings) {

        this.UUID = UUID;
        setDeaths(deaths);
        setPlaced(placedBlocks);
        setDestroyed(destroyed);
        setSettings(settings);
        stringToBool();
    }

    @Id
    @Column
    private String UUID;

    @Column
    private int deaths;

    @Column
    private int placed;

    @Column
    private int destroyed;

    @Column
    private String settings;

...
}

Test //This works

@Test
    public void abc(){
        SessionFactory sessionFactory;
        Properties properties = new Properties();

        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.setProperty("connection.driver_class", "com.mysql.cl.jdbc.Driver");
        properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/alldatanew");
        properties.setProperty("hibernate.connection.username", "root");
        properties.setProperty("hibernate.connection.password", "");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("current_session_context_class", "thread");

        try {
            sessionFactory = new Configuration()
                    .addProperties(properties)
                    .addAnnotatedClass(PlayerStats.class)
                    .buildSessionFactory();
        } catch (final Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        Session session = sessionFactory.openSession();
        SelectionQuery<PlayerStats> query = session.createSelectionQuery("FROM PlayerStats as P WHERE P.UUID = :uuid", PlayerStats.class);
        query.setParameter("uuid", "test3");
        PlayerStats playerStats = query.getSingleResult();
        System.out.println(playerStats.getUUID());


    }

PlayerStatsStore //This do not work

public PlayerStats findByUUID(String uuid) {

        try (Session session = openSession()) {
            SelectionQuery<PlayerStats> query = session.createSelectionQuery("FROM PlayerStats as P WHERE P.UUID = :uuid", PlayerStats.class);
            query.setParameter("uuid", uuid);
            PlayerStats playerStats = query.getSingleResult();
            return playerStats;
        } catch (final NoResultException exc) {
            return null;
        }
    }

The exception Exception Exception

So my question is why it works inside of the test and not after compiling? What am I doing wrong?

Regards, and thank you very much

I tried to solve my problem with the @NamedQuery annotation. This also doesn't work. So maybe my problem is at the SessionFactory init.

SessionFactory creation:

public SessionFactory createSessionFactory(Class<?>... clazz) {

        Properties properties = new Properties();

        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.setProperty("connection.driver_class", "com.mysql.cl.jdbc.Driver");
        properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/alldatanew");
        properties.setProperty("hibernate.connection.username", "root");
        properties.setProperty("hibernate.connection.password", "");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("current_session_context_class", "thread");
        properties.setProperty("hibernate.show_sql", "true");

        try {
            Configuration configuration = new Configuration();
            configuration.addProperties(properties);
            for (Class<?> c : clazz) {
                configuration.addAnnotatedClass(c.getClass());
            }
            return configuration.buildSessionFactory();
        } catch (final Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

CodePudding user response:

By using "Class<?>... clazz" to collect the classes which should be added to the configuration. The classes get stored in a list of java.lang.Class and not the actuall class. So now I created a builder class, where I can add the classes successively.

Thanks to @ewramner.

CodePudding user response:

Looking closer I think I spotted the bug in your original code. Change:

for (Class<?> c : clazz) {
    configuration.addAnnotatedClass(c.getClass());
}

To:

for (Class<?> c : clazz) {
    configuration.addAnnotatedClass(c);
}

You are already passing the classes, so the old code is registering the class of the class.

  • Related