Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Constructor Mapping with Spring JPA Repositories

I have a Spring repository as follows:

import org.springframework.data.repository.Repository;
import org.springframework.stereotype.Component;

import com.test.domain.My;

@Component
public interface MyRepository extends Repository<My, String> {

    My findOne(String code);

    My findByName(String name);

}

The entity class is:

import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown=true)
@Entity
@Table(name="vMy", schema="test")
@SqlResultSetMapping(
    name="something",
    classes = {
        @ConstructorResult(targetClass = My.class,
            columns={
                @ColumnResult(name = "myCode", type = String.class),
                @ColumnResult(name = "myShortName", type = String.class)
             }
        )
    }
)
public class My {

    @Id
    @Column(name = "myCode")
    private final String code;

    @Column(name = "myShortName")
    private final String name;

   public My(String code, String name) {
        this.code = code;
        this.name = name;
   }

   @JsonCreator()
   public My(@JsonProperty("My_c") String code) {
       this.code = code;
       this.name = null;
  }

  public String getCode() {
      return code;
  }

  public String getName() {
      return name;
  }

  @Override
    public String toString() {
       return "{code: " + code + ", name: " + name + "}";
   }    
 } 

When findOne or findByName is invoked, the following error is given:

   org.hibernate.InstantiationException: No default constructor for entity 

How can I use Spring JPA repository and not have a default constructor? I would like to keep the instance fields, code and name, final.

like image 673
James Avatar asked Sep 27 '22 13:09

James


1 Answers

I would create a separate class called MyDto which has the JSON stuff, but not the Entity annotations. Make it's fields final as well.

Then your repository methods would be something like this:

@Query("SELECT new MyDto(m.code, m.name) FROM My m WHERE m.code = :code")
public MyDto findByCode(@Param("code") String code);

That way, you are only using the My Entity class to give you the mapping to the database columns, not creating an instance of My.


EDIT: Another approach (as detailed here) is to use the Entity class itself as the DTO.

So your query method could look like this:

@Query("SELECT new My(m.code, m.name) FROM My m WHERE m.code = :code")
public My findByCode(@Param("code") String code);

This has the advantage of not having to create a separate DTO class.

like image 128
DuncanKinnear Avatar answered Oct 11 '22 17:10

DuncanKinnear