0

I have a table where the table structure is like this

package com.abc.domain;


@Entity
@Table(
    name = "ENTITY_DTL"
)

public class MyEntityDtl implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final int NATLTY_CD_LENGTH = 3;


    @Id
    @Column(
        name = "MY_ENTITY_DTL_ID",
        unique = true,
        nullable = false
    )
    @GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "myEntityDtlIdSeq"
    )
    @SequenceGenerator(
        name = "myEntityDtlIdSeq",
        sequenceName = "MY_ENTITY_DTL_ID_SEQ",
        allocationSize = 1
    )
    private Long MyEntityDtlId;



    @ManyToOne(
        fetch = FetchType.LAZY
    )
    @JoinColumns({@JoinColumn(
    name = "ENTITY_ID",
    referencedColumnName = "ENTITY_ID",
    nullable = false
), @JoinColumn(
    name = "ENTITY_TYPE_CD",
    referencedColumnName = "ENTITY_TYPE_CD",
    nullable = false
)})
    private MyEntity myEntity;


    /* 
    other columns and their getter setter 
    */


}

Here- what i want to achieve is - something like this

Join MyEntityDtl table with Abc table using this columns - ENTITY_ID and ENTITY_TYPE_CD .

How can i achieve it without using MyEntity table in the query.

As that will cost me additional join in this situation

Any help is appreciated, thank you in advance

4
  • Why do you want to avoid the extra join ? As long as it is in the way many -> one, it won't really cost anything in term of performance (check you didn't forget index though). Unless I'm missing something some might call that "premature optimization". Commented Jul 15, 2019 at 7:26
  • The only way I know is to add your Abc table as another ManyToOne association to your MyEntityDtl table Commented Jul 15, 2019 at 8:33
  • @Walfrat - you are correct , as the fetch type lazy is true - fetch = FetchType.LAZY , i can add a join and it won't affect the performance also Commented Jul 15, 2019 at 12:05
  • posted the answer below - for createQuery using entity manager Commented Jul 15, 2019 at 12:06

1 Answer 1

1

As the fetch type is lazy , i van directly make use of join column for creating the join, it will not affect the query performance becuase of fetch type lzay -

 Query query = getEntityManager().createQuery("SELECT abc FROM ABC abc, MyEntityDtl medtl join medtl.myEntity  me " +
                        " WHERE vsf.rqstId = me.entityId AND  " +
                        " me.entityTypeCd = '?' AND " +
                        " medtl.SOME_OTHER_COLUMN= '?' " ;
                query.setParameter("entityTypeCd", VALUE);
                query.setParameter("SOME_OTHER_COLUMN", VALUE);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.