Hibernate doesn't see the one entity in ten in certain package

Viewed 85

Problem:

I have 10 entities in one package com.trykote.tg.database.dto. The One of them is com.trykote.tg.database.dto.TgGroupEntity. When I ran application I got an error "MappingException: Unknown entity: com.trykote.tg.database.dto.TgGroupEntity".

But if i move this entity class to ANY other package (e.g. com.trykote.tg.database.dto.group, com.trykote.tg.database) — that works fine. If I just rename this to ANY other classname(e.g. Foo, MyTgGroupCertainEntity) — it doesn't work.

This's for real weird problem only with that's entity. Others work correctly. I've tried about 5 versions of Hibernate lib — the problem exists in each one.

Video bug demo for lazy [MP4 h264 2MB]: https://mega.nz/file/KZ52TBSL#6xf5FkrRROAqtreN5YUb8LHP2-SRF0tPDUgx3OisGeg

Question: What the hell is it? What i'm doing wrong or this is just some bug on Hibernate side? I can solve this problem just by moving entity to any other package, but really what is this?

Listings:

TgGroupEntity:

package com.trykote.tg.database.dto;

import com.trykote.tg.database.dto.group.VkGroupEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "tg_groups")
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class TgGroupEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tg_group_seq")
    @SequenceGenerator(name = "tg_group_seq", sequenceName = "tg_groups_id_seq", allocationSize = 1)
    @Column(name = "id")
    private int id;

    @Column(name = "tggid")
    private long tgGroupId;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "tgGroupEntity")
    @Fetch(value = FetchMode.SUBSELECT)
    private List<VkGroupEntity> vkGroupEntity;
}

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://X.X.X.X:5432/sharedfeedsdb</property>
        <property name="connection.username">sharedfeeds</property>
        <property name="connection.password">XXX</property>
        <property name="show_sql">false</property>

        <mapping class="com.trykote.tg.database.dto.group.VkGroupEntity"/>
        <mapping class="com.trykote.tg.database.dto.TgGroupEntity"/> <!-- HERE IS IT -->
        <mapping class="com.trykote.tg.database.dto.UserEntity"/>
        <mapping class="com.trykote.tg.database.dto.UserModeEntity"/>
        <mapping class="com.trykote.tg.database.dto.UserEmotionEntity"/>
        <mapping class="com.trykote.tg.database.dto.MemeEntity"/>
        <mapping class="com.trykote.tg.database.dto.SourceEntity"/>
        <mapping class="com.trykote.tg.database.dto.AttachmentEntity"/>
        <mapping class="com.trykote.tg.database.dto.BlackListSourceEntity"/>
        <mapping class="com.trykote.tg.database.dto.VkTokenEntity"/>
    </session-factory>
</hibernate-configuration>

Hibernate configuration in code:

public class HibernateUtil {
    private final StandardServiceRegistry registry;
    private final SessionFactory sessionFactory;

    public HibernateUtil() {
        this.registry = new StandardServiceRegistryBuilder()
                .configure()
                .build();
        this.sessionFactory = new MetadataSources(registry)
                .buildMetadata()
                .buildSessionFactory();
    }

    public SessionFactory sessionFactory() {
        return sessionFactory;
    }
}

Full stack trace:

org.hibernate.MappingException: Unknown entity: com.trykote.tg.database.dto.TgGroupEntity
 
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:704)
    at org.hibernate.internal.SessionFactoryImpl.getIdentifierType(SessionFactoryImpl.java:733)
    at org.hibernate.type.EntityType.getIdentifierType(EntityType.java:603)
    at org.hibernate.type.EntityType.getIdentifierOrUniqueKeyType(EntityType.java:643)
    at org.hibernate.type.EntityType.requireIdentifierOrUniqueKeyType(EntityType.java:767)
    at org.hibernate.type.ManyToOneType.getColumnSpan(ManyToOneType.java:142)
    at org.hibernate.tuple.entity.EntityMetamodel.create(EntityMetamodel.java:488)
    at org.hibernate.tuple.entity.EntityMetamodel.buildGenerationStrategyPair(EntityMetamodel.java:454)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:287)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:612)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:126)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
    at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:181)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:312)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471)
    at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:195)
    at com.trykote.tg.database.HibernateUtil.<init>(HibernateUtil.java:21)
    at com.trykote.tg.database.dao.UserServiceTest.<init>(UserServiceTest.java:14)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260)
    at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
0 Answers
Related