java songas,spring data jpa createNativeQuery 错误 Unknown entity

springdatajpa本地查询的时候,报错:org.hibernate.MappingException: Unknown entity: com.hzxc.guesssong.model.QuestionModel

解决办法

1.自定义对象类上添加@Entity注解,在其中一个属性上添加@Id标识注解。

2.如果jpa命名策略为: jpa.naming.strategy: org.hibernate.cfg.ImprovedNamingStrategy,则sql语句中字段用下划线表示单词连接符。例如:java: userName = > sql: user_name.

示例:

import javax.persistence.Entity;

import javax.persistence.Id;

/**

* Created by hdwang on 2018/6/6.

* 问题模型

*/

@Entity

public class QuestionModel {

@Id

private int questionId;

private int questionType;

private int questionLevel;

private int questionStatus;

private String options;

private String answer;

private int songId;

private String songSrcId;

private String songName;

private String songAuthor;

private String songSourceUrl;

private String songUrl;

private int songStatus;

public int getQuestionId() {

return questionId;

}

public void setQuestionId(int questionId) {

this.questionId = questionId;

}

public int getQuestionType() {

return questionType;

}

public void setQuestionType(int questionType) {

this.questionType = questionType;

}

public int getQuestionLevel() {

return questionLevel;

}

public void setQuestionLevel(int questionLevel) {

this.questionLevel = questionLevel;

}

public int getQuestionStatus() {

return questionStatus;

}

public void setQuestionStatus(int questionStatus) {

this.questionStatus = questionStatus;

}

public String getOptions() {

return options;

}

public void setOptions(String options) {

this.options = options;

}

public String getAnswer() {

return answer;

}

public void setAnswer(String answer) {

this.answer = answer;

}

public int getSongId() {

return songId;

}

public void setSongId(int songId) {

this.songId = songId;

}

public String getSongSrcId() {

return songSrcId;

}

public void setSongSrcId(String songSrcId) {

this.songSrcId = songSrcId;

}

public String getSongName() {

return songName;

}

public void setSongName(String songName) {

this.songName = songName;

}

public String getSongAuthor() {

return songAuthor;

}

public void setSongAuthor(String songAuthor) {

this.songAuthor = songAuthor;

}

public String getSongSourceUrl() {

return songSourceUrl;

}

public void setSongSourceUrl(String songSourceUrl) {

this.songSourceUrl = songSourceUrl;

}

public String getSongUrl() {

return songUrl;

}

public void setSongUrl(String songUrl) {

this.songUrl = songUrl;

}

public int getSongStatus() {

return songStatus;

}

public void setSongStatus(int songStatus) {

this.songStatus = songStatus;

}

}

@Service

public class QuestionQueryServiceImpl implements QuestionQueryService {

@PersistenceContext

EntityManager entityManager;

@Override

public QuestionVO getOneRandomQuestionByLevel(int level) {

String sql = "select q1.id as question_id,q1.type as question_type,q1.`level` as question_level,q1.status as question_status,q1.options,q1.answer,"

+" s.id as song_id,s.song_src_id as song_src_id,s.name as song_name,s.author as song_author,s.source_url as song_source_url,s.url as song_url,s.status as song_status "

+ " from question q1 inner join ( "

+ " select (min(q2.id) + round(rand()*(max(q2.id) - min(q2.id)))) as id from question q2 where q2.`level`= :level"

+ " ) as t on q1.id >= t.id inner join song as s on q1.songid=s.id "

+ " limit 1;";

Map params = new HashMap<>();

params.put("level",level);

Query query = this.entityManager.createNativeQuery(sql, QuestionModel.class);

this.setParameters(query,params);

QuestionModel questionModel = (QuestionModel) query.getSingleResult();

QuestionVO vo = new QuestionVO();

this.convertQuestionModelToQuestionVO(questionModel,vo);

return vo;

}

private void convertQuestionModelToQuestionVO(QuestionModel model,QuestionVO vo){

vo.setQuestionId(model.getQuestionId());

vo.setQuestionLevel(model.getQuestionLevel());

vo.setQuestionType(QuestionType.valueOf(model.getQuestionType()).getCnName());

vo.setSongUrl(model.getQuestionStatus()==1?model.getSongUrl():model.getSongSourceUrl());

vo.setOptions(JSONArray.parseArray(model.getOptions(),String.class));

}

/**

* 给hql参数设置值

* @param query 查询

* @param params 参数

*/

private void setParameters(Query query, Map params){

for(Map.Entry entry:params.entrySet()){

query.setParameter(entry.getKey(),entry.getValue());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值