Hibernate join cannot cast exception many to many

kamil.rak

I got a question regarding hibernate. I have to classes with many to many relationship and try to make a select. The problem is I am getting an exception. Can you help?

@Entity(name = "pracownik")
@Inheritance(strategy = InheritanceType.JOINED)
@Proxy(lazy = false)
public class Pracownik extends Osoba {

@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "grafikpracownika", joinColumns = { @JoinColumn(name = "idosoby") }, 
inverseJoinColumns = { @JoinColumn(name = "idgrafiku") })
private List<Grafik> grafiki = new ArrayList<>();
}

The second entity

@Entity (name = "grafik")
@Proxy(lazy = false)
public class Grafik {

@ManyToMany (mappedBy = "grafiki",cascade = { CascadeType.ALL })
private List <Pracownik> pracownik  = new ArrayList<>();


}

The method I developed is:

    public List<Pracownik> getWszyscyPracownicyGrafiku() {
    List<Pracownik> pracownicy = new ArrayList<>();
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    Query query = session.createQuery("from pracownik as p join p.grafiki");
    pracownicy = query.list();
    transaction.commit();
    return pracownicy;
}

And the exception is:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.Pracownik

Any idea what is wrong?
I also would like to add a "where" but it should be easy after I get rid of this exception. I also tried with "normal" sql

SELECT * from pracownik p join grafikpracownika g on p.idosoby = g.idosoby where idgrafiku = 6

but what I am getting is:

org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [idosoby] during auto-discovery of a native-sql query
axtavt

You're selecting from two tables without explicit select clause, therefore Hibernate produces a list of tuples (Pracownik, Grafik) as a result.

If you want only Pracowniks (i.e. join is needed to create condition on p.grafiki in where), use

select distinct p from pracownik as p join p.grafiki

If join is used to instruct Hibernate to fetch associated Grafiks, use join fetch:

from pracownik as p join fetch p.grafiki

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate join cannot cast exception many to many

From Dev

Hibernate, Use a constant in a many to many join

From Dev

Hibernate many-to-many join table not populating

From Dev

Hibernate Many To Many Mapping Persistence Exception

From Dev

Schema Exception Hibernate One to Many

From Dev

Hibernate many-to-many mapping + join table update not working

From Dev

Hibernate Many-To-Many join table not persisting for inherited Entity

From Dev

Hibernate Many-to-Many with join-class Cascading issue

From Dev

Hibernate many-to-many association not updating join table

From Dev

Initializing many-to-many association in Hibernate with join table

From Dev

HIbernate many to one join criteria query

From Dev

HIbernate many to one join criteria query

From Dev

How to query join one to many in hibernate HQL

From Dev

Hibernate one to many relationship with join table with addition columns in join table

From Dev

Understanding many to many mapping in hibernate

From Dev

Hibernate many to many proper delete

From Dev

Hibernate Many-To-Many. Not saving

From Dev

Hibernate query on many to many mapping

From Dev

Hibernate many to many like clause

From Dev

Many to Many relation with join table

From Dev

Many to many join Php/mysql

From Dev

How to join many to many in createQuery()

From Dev

Doctrine many to many left join

From Dev

Efficient Hibernate criteria for join returning many partial duplicates

From Dev

Getting Referenced property not a (One|Many)ToOne Hibernate Exception

From Dev

Duplicate Entry Exception: Spring Hibernate/JPA cascade save Many To One

From Dev

class cast exception in hibernate

From Dev

Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

From Dev

JPA/Hibernate: Map many-to-many relationship when join table has own primary key

Related Related

  1. 1

    Hibernate join cannot cast exception many to many

  2. 2

    Hibernate, Use a constant in a many to many join

  3. 3

    Hibernate many-to-many join table not populating

  4. 4

    Hibernate Many To Many Mapping Persistence Exception

  5. 5

    Schema Exception Hibernate One to Many

  6. 6

    Hibernate many-to-many mapping + join table update not working

  7. 7

    Hibernate Many-To-Many join table not persisting for inherited Entity

  8. 8

    Hibernate Many-to-Many with join-class Cascading issue

  9. 9

    Hibernate many-to-many association not updating join table

  10. 10

    Initializing many-to-many association in Hibernate with join table

  11. 11

    HIbernate many to one join criteria query

  12. 12

    HIbernate many to one join criteria query

  13. 13

    How to query join one to many in hibernate HQL

  14. 14

    Hibernate one to many relationship with join table with addition columns in join table

  15. 15

    Understanding many to many mapping in hibernate

  16. 16

    Hibernate many to many proper delete

  17. 17

    Hibernate Many-To-Many. Not saving

  18. 18

    Hibernate query on many to many mapping

  19. 19

    Hibernate many to many like clause

  20. 20

    Many to Many relation with join table

  21. 21

    Many to many join Php/mysql

  22. 22

    How to join many to many in createQuery()

  23. 23

    Doctrine many to many left join

  24. 24

    Efficient Hibernate criteria for join returning many partial duplicates

  25. 25

    Getting Referenced property not a (One|Many)ToOne Hibernate Exception

  26. 26

    Duplicate Entry Exception: Spring Hibernate/JPA cascade save Many To One

  27. 27

    class cast exception in hibernate

  28. 28

    Does a hibernate many to many relationship have to use the @ManyToMany annotation and use a join table?

  29. 29

    JPA/Hibernate: Map many-to-many relationship when join table has own primary key

HotTag

Archive