简体   繁体   English

使用带有Spring Data JPA的自定义查询返回自定义对象

[英]Return custom Object using custom Query with Spring Data JPA

I have researched question and couldn't find appropriate answer. 我研究了问题,找不到合适的答案。

I am trying to return only certain columns from table using custom query with Spring Data JPA in my Spring Rest application. 我试图在Spring Rest应用程序中使用带有Spring Data JPA的自定义查询从表中仅返回某些列。 However query always throws exception when executed. 但是查询在执行时总是引发异常。

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [org.forum.api.model.Message] org.springframework.core.convert.ConverterNotFoundException:未找到能够从[java.lang.String]类型转换为[org.forum.api.model.Message]类型的转换器

I know that it is possible to use String but why are Message objects not Serialized properly into JSON even thought I have created model for it in a sub-package of Spring Boot main ? 我知道可以使用String,但是为什么即使我在Spring Boot main的子包中为它创建了模型,消息对象却仍未正确序列化为JSON?

Here is my model class. 这是我的模型课。

@Entity
@Table(name="message")
public class Message {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "text_id")
    private long id;

    @NotNull
    private String author;

    @NotNull
    private String text;

    @NotNull
    private String recepient;

    public long getId() {return id;}

    public void setId(long id) {this.id = id;}

    public String getAuthor() {return author;}

    public void setAuthor(String author) {this.author = author;}

    public String getText() {return text;}

    public void setText(String text) {this.text = text;}

    public String getRecepient() {return recepient;}

    public void setRecepient(String recepient) {this.recepient = recepient;}

}

Here is controller class. 这是控制器类。

@RestController
@RequestMapping("/api")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/message/{id}")
    public Message getMessageTextById(@PathVariable(value="id") Long id) {
        return messageService.getMessageTextById(id);       
    }

}

Here is service class. 这是服务等级。

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageRepository messageRepo;

    @Override
    public Message getMessageTextById(Long id) {        
        return messageRepo.findMessageTextById(id);     
    }

}

Here is Repository Class 这是存储库类

@Repository
public interface MessageRepository extends JpaRepository<Message, Long> {


    @Query("SELECT m.author, m.text FROM Message m WHERE m.id = :id")
    Message findMessageTextById(@Param("id") Long id);

}

If you want to retrieve only certain columns you can use a simple bean class: 如果只想检索某些列,则可以使用一个简单的bean类:

public class CustomMessage{
  private String author;
  private String text;

  public CustomMessage(String author, String text) {
    this.author = author;
    this.author = text;
  }
}

Then return a bean instance from your repository: 然后从您的存储库返回一个bean实例:

@Query("SELECT new path_to_class.CustomMessage(m.author, m.text) FROM Message m WHERE m.id = :id")

Or retrieve a map: 或检索地图:

 @Query("SELECT new map(m.author as author, m.text as text) FROM Message m WHERE m.id = :id")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM