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

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

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

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

Q&A

解決済

1回答

242閲覧

SpringJpaでのデータベースについて

退会済みユーザー

退会済みユーザー

総合スコア0

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

0グッド

0クリップ

投稿2018/03/28 14:02

編集2018/04/01 08:45

SpringJpaのローカルDBについて聞きたいことがあります。

Java

1@Entity 2@Table(name="TechSearchList") 3public class TechSearchListEntity implements Serializable { 4 5 @Id 6 @Column 7 private int id; 8 9 @Column(length=50, nullable=false) 10 private String classificationName; 11 12 @Column(length=2, nullable=false) 13 private String classificationCd; 14 15 @Column(nullable=false) 16 private Date updateDate; 17 18 public int getId() { 19 return id; 20 } 21 22 public void setId(int id) { 23 this.id = id; 24 } 25 26 public String getClassificationName() { 27 return classificationName; 28 } 29 30 public void setClassificationName(String classificationName) { 31 this.classificationName = classificationName; 32 } 33 34 public String getClassificationCd() { 35 return classificationCd; 36 } 37 38 public void setClassificationCd(String classificationCd) { 39 this.classificationCd = classificationCd; 40 } 41}

上記がDBの値を取得したり、更新などを行うためのEntityです。
ローカル用のDBを作成し、TechSearchListというテーブルを作成しました。
データの取得等はJpaRepositoryを使用しています。
Tomcatを起動すると、insertしたデータが消えているので、テーブルをみてみると、TechSearchListEntityというテーブルができていて、値を入れると、Tomcatを起動してもデータは消えないようでした。
@Table(name="TechSearchList")を定義していたので、TechSearchListというテーブルを見ていると思ったのですが、クラス名のテーブルが勝手に作られるものなのでしょうか。
ご存知の方がいたら教えていただきたいです。

設定ファイルを追記します。

persistence.xml

persistence.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<persistence version="2.1" 3 xmlns="http://xmlns.jcp.org/xml/ns/persistence" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 6 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 7 8 <persistence-unit name="persistenceUnit" 9 transaction-type="RESOURCE_LOCAL"> 10 <provider>org.hibernate.ejb.HibernatePersistence</provider> 11 <properties> 12 <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> 13 <property name="hibernate.hbm2ddl.auto" value="create" /> 14 <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> 15 <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/××××" /> 16 <property name="hibernate.connection.password" value="××××××" /> 17 <property name="hibernate.connection.username" value="user1" /> 18 </properties> 19 </persistence-unit> 20</persistence>

application-config.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> --> <context:property-placeholder location="classpath:spring/database.properties" /> <context:annotation-config /> <context:component-scan base-package="co.jp.TechMemo" /> <jpa:repositories base-package="co.jp.TechMemo.Repository" /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> </bean> </property> <property name="persistenceUnitName" value="persistenceUnit" /> <property name="dataSource" ref="dataSource" /> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> </bean> <!-- jdbc --> <bean class ="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> <property name="driverClassName" value="${database.driverClassName}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean> <jpa:repositories base-package="co.jp.springbook.TechMemo.Repository" /> </beans>

mvc-config.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Uncomment and your base-package here --> <context:component-scan base-package="co.jp.TechMemo.controller" /> <context:component-scan base-package="co.jp.TechMemo.Service.TechSearchSerive" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2018/03/28 23:53

設定によりけり
guest

回答1

0

ベストアンサー

persistence.xmlの下記が原因でした。

<property name="hibernate.hbm2ddl.auto" value="create" />

value="update"にするとうまくいきました。

投稿2018/07/30 14:39

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問