質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

3448閲覧

SpringBootでDBから取得処理をしたいが、テーブル名 is not mappedになる

circular2016

総合スコア52

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/07/19 15:28

前提・実現したいこと

SpringBootで、論理削除フラグ=0という条件で、テーブルからデータを取得してきたいです。

EntityManagerを使って実装をしようとしています。

SpringBootを起動し、
http://localhost:8080/list を叩いたところ、下記のエラーが発生しました。

発生している問題・エラーメッセージ

org.hibernate.hql.internal.ast.QuerySyntaxException: schedule is not mapped at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:77) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final] 以下略。

該当のソースコード

コントローラ

Java

1package com.example.demo.controller; 2 3import java.sql.Timestamp; 4import java.text.ParseException; 5import java.text.SimpleDateFormat; 6import java.util.Date; 7import java.util.List; 8 9import javax.persistence.EntityManager; 10import javax.persistence.PersistenceContext; 11import javax.persistence.criteria.CriteriaBuilder; 12import javax.persistence.criteria.CriteriaQuery; 13import javax.persistence.criteria.Predicate; 14import javax.persistence.criteria.Root; 15 16import org.springframework.beans.factory.annotation.Autowired; 17import org.springframework.data.jpa.domain.Specification; 18import org.springframework.stereotype.Controller; 19import org.springframework.transaction.annotation.Transactional; 20import org.springframework.ui.Model; 21import org.springframework.web.bind.annotation.GetMapping; 22import org.springframework.web.bind.annotation.ModelAttribute; 23//import org.springframework.web.bind.annotation.GetMapping; 24import org.springframework.web.bind.annotation.RequestMapping; 25import org.springframework.web.bind.annotation.RequestMethod; 26import org.springframework.web.bind.annotation.RequestParam; 27import org.springframework.web.servlet.ModelAndView; 28 29import com.example.demo.dao.ScheduleDaoImpl; 30import com.example.demo.entity.ScheduleEntity; 31import com.example.demo.repository.ScheduleRepository; 32 33@Controller 34public class ScheduleController { 35 36 @Autowired 37 ScheduleRepository scheduleRepository; 38 39 @Autowired 40 ScheduleDaoImpl dao; 41 42 @GetMapping("/regist") 43 public String getLogin(Model model) { 44 45 return "regist"; 46 } 47 48 @RequestMapping(value = "/list", method = RequestMethod.GET) 49 @Transactional(readOnly = true) 50 public ModelAndView getAllSchedule(ModelAndView mav) { 51 // (3)全件データの取得 52 // すべてのデータを取得するには、(3)のようにfindAllメソッドで取得できます。 53 List<ScheduleEntity> scheduleList = dao.findDisabledRecords(); 54 mav.setViewName("index"); 55 mav.addObject("scheduleList", scheduleList); 56 57 return mav; 58 } 59} 60

Dao

Java

1package com.example.demo.dao; 2 3import java.io.Serializable; 4import java.util.List; 5 6public interface ScheduleDao<T> extends Serializable { 7 8 public List<T> findDisabledRecords(); 9} 10

Daoの実装クラス

Java

1package com.example.demo.dao; 2 3import java.util.List; 4 5import javax.persistence.EntityManager; 6import javax.persistence.PersistenceContext; 7import javax.persistence.criteria.CriteriaBuilder; 8import javax.persistence.criteria.CriteriaQuery; 9import javax.persistence.criteria.Predicate; 10import javax.persistence.criteria.Root; 11 12import org.springframework.data.jpa.domain.Specification; 13import org.springframework.stereotype.Repository; 14 15import com.example.demo.entity.ScheduleEntity; 16 17@Repository 18public class ScheduleDaoImpl implements ScheduleDao<ScheduleEntity> { 19 20 private static final long serialVersionUID = 1L; 21 22 public ScheduleDaoImpl() { 23 super(); 24 } 25 26 @PersistenceContext 27 private EntityManager entityManager; 28 29 public ScheduleDaoImpl(EntityManager manager) { 30 this(); 31 entityManager = manager; 32 } 33 34 @Override 35 @SuppressWarnings("unchecked") 36 public List<ScheduleEntity> findDisabledRecords(){ 37 String flg = "0"; 38 return (List<ScheduleEntity>)entityManager.createQuery("from Schedule where deleteFlg =" + flg, ScheduleEntity.class).getResultList(); 39 } 40} 41

Entity

Java

1package com.example.demo.entity; 2 3import java.sql.Timestamp; 4 5import javax.persistence.Column; 6import javax.persistence.Entity; 7import javax.persistence.GeneratedValue; 8import javax.persistence.GenerationType; 9import javax.persistence.Id; 10import javax.persistence.Table; 11 12 13@Entity 14@Table(name="SCHEDULE") 15public class ScheduleEntity { 16 17 /* 18 * 一意に割り振られるID 19 */ 20 @Id 21 @Column (name="SCHEDULE_ID", nullable=false) 22 @GeneratedValue(strategy=GenerationType.IDENTITY) 23 private String scheduleId; 24 25 /* 26 * 予定の件名 27 */ 28 @Column (name="TITLE", nullable=false, length=20) 29 private String title; 30 31 /* 32 * 予定の開始日付時刻 33 */ 34 @Column (name="START_DATE", nullable=false) 35 private Timestamp startDate; 36 37 /* 38 * 予定の終了日付時刻 39 */ 40 @Column (name="END_DATE") 41 private Timestamp endDate; 42 43 /* 44 * 予定の場所 45 */ 46 @Column (name="PLACE", length=20) 47 private String place; 48 49 /* 50 * 予定の詳細 51 */ 52 @Column (name="DETAIL", length=100) 53 private String detail; 54 55 /* 56 * 登録者 57 */ 58 @Column (name="USER_ID", nullable=false) 59 private Integer RegistUser; 60 61 /* 62 * 予定の登録日時 63 */ 64 @Column (name="REGIST_TIME", nullable=false) 65 private Timestamp registTime; 66 67 /* 68 * 予定の更新日時 69 */ 70 @Column (name="UPDATE_TIME") 71 private Timestamp updateTime; 72 73 /* 74 * 削除フラグ 75 */ 76 @Column (name="DELETE_FLG", nullable=false) 77 private String deleteFlg; 78 79 public String getScheduleId() { 80 return scheduleId; 81 } 82 83 public void setScheduleId(String scheduleId) { 84 this.scheduleId = scheduleId; 85 } 86 87 public String getTitle() { 88 return title; 89 } 90 91 public void setTitle(String title) { 92 this.title = title; 93 } 94 95 public Timestamp getStartDate() { 96 return startDate; 97 } 98 99 public void setStartDate(Timestamp startDate) { 100 this.startDate = startDate; 101 } 102 103 public Timestamp getEndDate() { 104 return endDate; 105 } 106 107 public void setEndDate(Timestamp endDate) { 108 this.endDate = endDate; 109 } 110 111 public String getPlace() { 112 return place; 113 } 114 115 public void setPlace(String place) { 116 this.place = place; 117 } 118 119 public String getDetail() { 120 return detail; 121 } 122 123 public void setDetail(String detail) { 124 this.detail = detail; 125 } 126 127 public Integer getRegistUser() { 128 return RegistUser; 129 } 130 131 public void setRegistUser(Integer registUser) { 132 RegistUser = registUser; 133 } 134 135 public Timestamp getRegistTime() { 136 return registTime; 137 } 138 139 public void setRegistTime(Timestamp registTime) { 140 this.registTime = registTime; 141 } 142 143 public Timestamp getUpdateTime() { 144 return updateTime; 145 } 146 147 public void setUpdateTime(Timestamp updateTime) { 148 this.updateTime = updateTime; 149 } 150 151 public String getDeleteFlg() { 152 return deleteFlg; 153 } 154 155 public void setDeleteFlg(String deleteFlg) { 156 this.deleteFlg = deleteFlg; 157 } 158 159}

テーブルデータ
イメージ説明

試したこと

複数のサイトを調べたのですが、全く手がかりが掴めませんでした。

補足情報(FW/ツールのバージョンなど)

PostgreSQLを使っています。
EntityManagerを使わないで登録処理は先に作成しており、そちらは正常に登録できています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YT0014

2021/07/20 13:32

EntityのTableとColumnのnameは大文字、実際のテーブルやカラムの名称は小文字となっているようですが、どんな意図があるのでしょうか? 深い意図がないのなら、Entityのnameを小文字に変更してみてください。
circular2016

2021/07/21 15:43

ありがとうございます。 小文字にしてみました。状況としては変わりはありませんでした。
guest

回答1

0

ベストアンサー

Java

1 @Override 2 @SuppressWarnings("unchecked") 3 public List<ScheduleEntity> findDisabledRecords(){ 4 String flg = "0"; 5 return (List<ScheduleEntity>)entityManager.createQuery("from Schedule where deleteFlg =" + flg, ScheduleEntity.class).getResultList(); 6 }

となっていますが、ここで指定するSQLでは、Java内のクラス名やプロパティ名を使用する必要があるので、テーブル名は、
ScheduleEntity
とするのが正解かと思われます。(未検証)

HQLクエリでHibernateテーブルがマップされていないエラー

投稿2021/07/22 02:21

YT0014

総合スコア1708

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

circular2016

2021/08/06 14:47

返信がだいぶ遅くなり、申し訳ございません。 >ここで指定するSQLでは、Java内のクラス名やプロパティ名を使用する必要があるので、テーブル名は、ScheduleEntity とするのが正解かと思われます。 こちらで解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問