0

Hi I have few table that listed below:

public class GroupEntity extends BaseEntity {

    @Id
    @SequenceGenerator(name = "beta_group_seq", sequenceName = "group_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_seq")
    @Column(name = "id")
    private Long id;

    @Column(name = "name", length = 1400)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private UserEntity userEntity;

    @Column(name = "status")
    private String status;
}

that have ManyToOne relation with UserEntity:

public class UserEntity extends BaseEntity {

    @Column(name = "user_code", nullable = false, unique = true)
    private String userCode;
}

Both the Entity extends the Basic Entity:

@Getter
@MappedSuperclass
public abstract class BaseEntity {

    @Column(name = "CREATED_BY", length = 8, updatable = false)
    private String createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATION_DATE", updatable = false)
    private Date creationDate;

    @Column(name = "LAST_UPDATED_BY", length = 8, updatable = false)
    private String lastUpdatedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_UPDATED_DATE", updatable = false)
    private Date lastUpdatedDate;
}

To get the userCode that is in UserEntity I wrote the below JPA Criteria Query:

 public List<GroupEntity> getData(final String userCode,
                                                                       final String currentGroupName, final String newGroupName) {

        final EntityManager entityManager = entityManagerProvider.get();
        final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        final CriteriaQuery<GroupEntity> cq = cb.createQuery(GroupEntity.class);
        final Root<GroupEntity> root = cq.from(GroupEntity.class);
        cq.select(root).where(cb.or(cb.equal(root.get("userCode"), userCode), cb.equal(root.get("name"),
                currentGroupName), cb.equal(root.get("name"), newGroupName)));
        return entityManager.createQuery(cq).getResultList();
    }

But got some internalFailure due to failure in above query:

Unable to locate Attribute  with the the given name [userCode] on this ManagedType [com.<servicename>.entity.BaseEntity]

I am not sure if its due to wrong Query that I have put above or due to some other ?

Thanks!!

1 Answers1

1

You must join the UserEntity to have a reference to build the where clause:

Root<GroupEntity> root = cq.from(GroupEntity.class);
Join<GroupEntity,UserEntity> user = root.join("userEntity");
cq.select(root).where(cb.or(
              cb.equal(user.get("userCode"), userCode), 
              cb.equal(root.get("name"), currentGroupName), 
              cb.equal(root.get("name"), newGroupName)));

Btw. you should consider to generate the metamodel for typesafe querying.

https://www.baeldung.com/hibernate-criteria-queries-metamodel

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • So this is the kind of first level join. How can be achieved for second level of Join ?For eg: let say `UserEntity` also have `UserAlphaEntity` type filed that have alphaId and I need to fetch AlphaId from `GroupEntity ` itself ? – Just_Do_It_123 Nov 29 '22 at 18:06
  • user.join("userAlphaEntity") It's the same – Simon Martinelli Nov 30 '22 at 06:31