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>
回答1件
あなたの回答
tips
プレビュー