question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Entity mapping not working as expected for non-PK referencedColumnName in JoinColumn

See original GitHub issue

Description

In an entity, I have a @ManyToOne mapping with a non-PK referencedColumnName and I’m trying to map the target entity into an entity view. Here is an attempt to simplify the setup:

@Entity
public class Cat {
    @Id
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(referencedColumnName = "code")
    private CatFood favoriteFood;
}

@Entity
public class CatFood extends AbstractBaseEntity implements Serializable
{
    @Id
    private Long id;
    @Column(nullable = false, unique = true)
    private Long code;
    private String brand;
}
@EntityView(Cat.class)
public interface CatView
{
    @IdMapping
    Long getId();
    String getName();
    @Mapping("favoriteFood")
    CatFood getFavoriteFood();   
}

        ID NAME         FAVORITEFOOD_CODE
---------- ------------ -----------------
         1 Lucy           23
         2 Tom             1
         3 Kitty                                                                               


        ID BRAND        CODE
---------- ------------ ----------
         1 Whiskas           23
         2 Purina             1

Expected behavior

When the entity is queried directly, the favorite food should be loaded:

Cat cat = cbf.create(em, Cat.class).where("id").eq(2L).getSingleResult();
assertThat(cat.getFavoriteFood(), is(not(nullValue())));
assertThat(cat.getFavoriteFood().getBrand(), is("Purina"));

When the entity view is queried, the favorite food should also be loaded:

CriteriaBuilder<Cat> cb = cbf.create(em, Cat.class).where("id").eq(2L);
CriteriaBuilder<CatView> viewBuilder = evm.applySetting(EntityViewSetting.create(CatView.class), cb);
CatView catView = viewBuilder.getSingleResult();
assertThat(catView .getFavoriteFood(), is(not(nullValue())));
assertThat(catView .getFavoriteFood().getBrand(), is("Purina"));

Actual behavior

When querying the Cat entity directly, the behavior is as expected. For the catView though, getFavoriteFood() returns null.

Worse, in my original and much more complex setup the method corresponding to getFavoriteFood() in this example returns the wrong entity if the “food code” in the FK column matches another row with an “id” of the same value. Unfortunately, I couldn’t reproduce this exact behavior with the simplified setup.

Environment

Version: 1.6.3 JPA-Provider: Hibernate 5.5.7.Final DBMS: Oracle Application Server: Apache Tomcat, Spring Framework

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cgraefecommented, Nov 10, 2021

Thanks again for your help and effort!

Ultimately, it will be better to use entity views all the way though because you can then fully control what is fetched and how, which entities not necessarily allow in all cases.

Generally, I agree! In my particular case, I am using Blaze Persistence to improve a large existing code base with some parts dating back more than 10 years. Being able to (at least temporarily) map entities into views is a nice feature that allows for a more gradual refactoring, imho.

0reactions
cgraefecommented, Nov 22, 2021

Good to know, at least. Thanks for looking into this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referenced column is not working correct with @ManyToOne ...
When I do it, I have another error(( Caused by: org.springframework.data.mapping.PropertyReferenceException: No property groups found for type ...
Read more >
Non-PK (unique) referencedColumnName in JoinColumn ...
Given the entities: public class PaynlTransaction extends IdIdentifiable implements Comparable { @Id @Column(name = "order_entry_id") ...
Read more >
referencedColumnName mapping problem - Google Groups
Hi,. It looks like Ebean doesn't support referencedColumnName in ManyToOne relations. It seems to require that the referenced column is an
Read more >
[HHH-7668] Wrong binding of @ManyToOne for non-PK ...
So I would expect that because of the unique constraint declared, there is no need for class A to be marked as Serializable...
Read more >
bug with @MapsId in single-column identifying relationship?
EclipseLink has problems mapping them with the following entity classes: ... @JoinColumn(name = "country_code", referencedColumnName ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found