zoukankan      html  css  js  c++  java
  • 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<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());
            }
        }
    
    }
  • 相关阅读:
    善用js 异步引入,大幅提升vue站点加载速度,媲美大厂流畅性
    react hooks useState 赋值优化解决方案
    vue hash模式下的微信授权详解
    图片之间没加空格有间隙的解决方案
    React 父组件重新渲染,子组件不需要渲染的三种性能优化方式(PureComponent,memo,shouldComponentUpdate);
    uniapp历史模式history配置
    vue下请求数据地址的封装
    h5 下ios适配底部小黑条,简单解决方案,只需一步
    overflow:hidden 失效问题
    h5下数字,字母不换行的解决方案
  • 原文地址:https://www.cnblogs.com/hdwang/p/9145805.html
Copyright © 2011-2022 走看看