Home > Net >  reflection native query returns LinkedHashSet with proxy
reflection native query returns LinkedHashSet with proxy

Time:02-10

I encounter the problem that I receive a linked hashset from my repository instead of a concrete java object.

I have three entities which are not related to each other with annotations. This is done on purpose since they are frequently updated by three different topics and I want to reduce errors due to foreign key constraint problems etc.

However, I want to do a select on these three tables and generate an object out of them. I decided to use an interface for reflection.

Setup

Entity 1

@Entity
@Getter
@Setter
public class EBA implements Serializable {

private static final long serialVersionUID = -1111361234567966021L;

@Id
@Column(name = "EBA")
private String eba;

@Column(name = "EBA_START")
private LocalDate ebaStart;

@Column(name = "EBA_End")
private LocalDate ebaEnd;

Entitiy 2

@Entity
@Getter
@Setter
public class Type implements Serializable {

private static final long serialVersionUID = -1111361234567966021L;

@Id
@Column(name = "TYPE")
private String type;

@Column(name = "EBA")
private String eba;

@Column(name = "MONT")
private String mont;

Entitiy 3 with embeddedId

@Entity
@Getter
@Setter
public class Mapping implements Serializable {

private static final long serialVersionUID = -1111361234567966021L;

@EmbeddedId
private Id id = new Id();

@Column(name = "LPROCESS")
private String lprocess;


@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Id implements Serializable {

private static final long serialVersionUID = 8325874757964361236L;

@Column(name = "P_CODE")
private String pCode;

@Column(name = "TYPE")
private String type;
}

Now I created a repository in order to get the required data from all three tables:

Repository

@Repository
public interface PToLEbaMappingRepository extends CrudRepository<LPWitPCodeMapping, LPWitPCodeMappingId> {

String QUERY = "select distinct "  
        "typ.EBA as eba, "  
        "mapp.CODE as pCode, "  
        "mapp.LCODE as lProcess, "  
        "pte.EBA_START as ebaStart, "  
        "pte.EBA_END as ebaEnd "  
        "FROM MAPPING mapp "  
        "JOIN TYPE type ON mapp.TYPE = type.TYPE "  
        "JOIN EBA pte ON typ.EBA = pte.EBA "  
        "WHERE mapp.P_CODE = :pCode "  
        "AND :end >= pte.EBA_START "  
        "AND :start <= pte.EBA_END "  
        "AND type.MONT = 'CFU'";

@Query(value = QUERY, nativeQuery = true)
Set<ReflectedObject> findByPCodeAndStartEnd(
        @Param("pCode") String pCode,
        @Param("start") LocalDate start,
        @Param("end") LocalDate end
);
}

My reflection interface is the following:

Interface

public interface ReflectedObject {
String getEba();
String getPCode();
String getLProcess();
LocalDate getEbaStart();
LocalDate getEbaStop();
}

Result

db.pToLEbaMappingRepository().findByPCodeAndStartEnd(pCode, start, end) = {LinkedHashSet@21383}  size = 4
3 = {$Proxy225@21388} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@28600efd"
2 = {$Proxy225@21387} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@596e91fe"
1 = {$Proxy225@21386} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@3a7b7551"
0 = {$Proxy225@21385} "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@30850927"

The size of the set I get back is correct. But the object is not readable and it is some sort of proxy, which I cannot convert to an object. I tried to hibernate "unproxy" the data, but it gave me the error that sun proxy cannot be cast to hibernate proxy.

I rather have the feeling that there is something wrong with my query. But I cant see any problem.

CodePudding user response:

  •  Tags:  
  • Related