0

I have an entity defined below, I am able to store all attributes, including vehicles and missingEntity in their correct tables, with an associated ID to this entity.

I am able to retrieve the list of vehicles, but when I try to retrieve the missingEntity associated with this user, it is always null.

Below is the definition of the user entity:

@Entity
@Table(name="table")
public class UserEntity {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private long userId;

    @Column(name="email")
    private String email;
    
    @OneToMany(mappedBy="userId", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private List<VehicleEntity> vehicles;
    
    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name = "id", referencedColumnName = "user_id")
    private MissingEntity missingEntity;
    
}

Below is the definition of the missingEntity:

@Entity
@Table(name="missing_entity")
public class MissingEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private long id;
    
    @Column(name="user_id")
    private int userId;
    
    @Column(name="missing_value")
    private String missingValue;
    
}

I save the user using the built in JPA method in my UserRepo, which extends JpaRepository, userRepo.saveAndFlush(userEntity);. I am able to retrieve the user and vehicles, but not the missingEntity.

I have verified in the database that the user_id column matches the id column of the user upon call to saveAndFlush(userEntity), which is the desired relation, and the same relation that vehicles use.

Edit: Upon further research, it seems that I need to set up a bidirectional mapping. I have updated the UserEntity missingEntity as follows:

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "missinEntity", joinColumns = {
        @JoinColumn(name = "id", referencedColumnName = "user_id")
},
inverseJoinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName = "id", unique = true)
})
private missingEntity missingEntity;

However, I now am receiving the following error:

org.hibernate.cfg.RecoverableException: Unable to find column with logical name: user_id in org.hibernate.mapping.Table(user) and its related supertables and secondary tables

Edit 2: I have updated the mapping once again to a bidirectional mapping see below:

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "missing_table", joinColumns = {
        @JoinColumn(name = "user_id", referencedColumnName = "id")
},
inverseJoinColumns = {
        @JoinColumn(name = "id", referencedColumnName = "user_id", unique = true)
})
private MissingEntity missingEntity;

However I now am seeing the same problem of the missingEntity being stored in the database with the correct user_id value, but being unable to retrieve that value based on the user id of the entity.

CS2016
  • 331
  • 1
  • 3
  • 15
  • You are trying to make `UserEntity` the owner of the relationship with `MissingEntity`, but this is not consistent with the structure of your DB. The dupe target contains (many) more details about the owning *vs* inverse side of a JPA relationship, and how set up mappings. The relationship *must* be mapped on the owning side (`MissingEntity`, per the DB structure). It *may* also be mapped on the inverse side (`UserEntity`). – John Bollinger Nov 17 '22 at 16:32

0 Answers0