spring data jpa createNativeQuery 错误 Unknow…
2018-06-18 00:23:15来源:未知 阅读 ()
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<String,Object> 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<String,Object> params){ for(Map.Entry<String,Object> entry:params.entrySet()){ query.setParameter(entry.getKey(),entry.getValue()); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash