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()); } } }