Home > Back-end >  How to get non related entity using JPA
How to get non related entity using JPA

Time:08-24

I have two entities

@Entity
public class Language {

@Id
@GeneratedValue
private int id;

private String name;

private String isoCode;

}

and the another entity



@Entity
public class MainEntity {

@Id
@GeneratedValue
private int id;

private String anotherField;

private Language language;  //(here I want to return the full entity)

}

The entities don't have a database relation just a string field, Now I want to do a query for MainEntity but I want to get the full entity

For example

Language

value
id 1
name English
isoCode EN

MainEntity

value
id a
name xyz
language EN

I have that repository

public interface MainRepository extends JpaRepository<MainEntity, Integer>{

User findAll();
}

but I want that when I do a search on my primary entity it brings me the entire language entity

for example

MyEntity.Language.getName()

CodePudding user response:

Use native query to read JPA objects? See this simplified example where you can use any valid native sql query syntax.

You could put Language getLanguage() function to MainEntity class.

String sql = "Select * From MyEntityTable Where id between (?1 and ?2)";
Query query = entityMgr.createNativeQuery(sql, MyEntity.class);
query.setParameter(1, 1001);
query.setParameter(2, 2002);
List<MyEntity> list = query.getResultList();

CodePudding user response:

If you don't want to make a reletion between MainEntity and Language you have to make a native query. (As is you can't make also a jpql query because you should have at least the String language in the MainEntity in order to make a join on String isoCode in Language but as I understand I think you don't want to do that). So you have to perform a native query returning an interface as stated here

public interface IntermediateObject {
    int getId();

    String getName();

    int getLanguageId();

    String getLanguageName();

    String getIsoCode();
}

In the repository:

@Query("select m.id as id, m.name as name, l.id as languageId, l.name as languageName, l.isoCode as isoCode MainEntity m join Language l on m.language = l.isoCode", native=true)
List<IntermediateObject> someMethod();

In the MainEntity or better another dto you have to declare a constructor:

public class AnotherDto {

    private int id;
    private String name;
    private LanguageDto language;

    public AnotherDto(int id, String name, int languageId, String languageName, String isoCOde) {
        this.id = id;
        this.name = name;
        this.language = new LanguageDto(languageId, languageName, isoCode);
    }

}
public class LanguageDto {
    private int id;
    private String name;
    private String isoCode;

    public LanguageDto(int id, String name, String isoCode) {
        this.id;
        this.name = name;
        this.isoCode = isoCode;
    }
}

And in the service layer you have to construct AnotherDto from IntermediateObject

public List<AnotherDto> someMethod() {
    return repository.someMethod().stream().map(i -> new AnotherDto(i.getId(), i.getName(), i.getLanguageId(), i.getLanguageName(), i.getIsoCode())).collect(Collectors.toList());
}

and now you have the language object populated.

Anyway I suggest to map the entities instead to do all of this.

  • Related