Created
February 13, 2014 13:45
-
-
Save kmb385/8975228 to your computer and use it in GitHub Desktop.
Spring Data/JPA Enum Mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:jpa="http://www.springframework.org/schema/data/jpa" | |
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd"> | |
<!-- Database --> | |
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | |
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> | |
<property name="url" value="jdbc:mysql://localhost:3306/to_thought_tutorial" /> | |
<property name="username" value="username" /> | |
<property name="password" value="password" /> | |
</bean> | |
<!-- Entity Manager --> | |
<bean id="entityManagerFactory" | |
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> | |
<property name="dataSource" ref="datasource" /> | |
<property name="persistenceUnitName" value="tothought-tutorial" /> | |
</bean> | |
<!-- Transaction Manager --> | |
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> | |
<property name="entityManagerFactory" ref="entityManagerFactory" /> | |
</bean> | |
<!-- Jpa Repositories --> | |
<jpa:repositories base-package="com.cloudfoundry.tothought.repositories"></jpa:repositories> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
version="1.0"> | |
<persistence-unit name="tothought-tutorial" transaction-type="RESOURCE_LOCAL"> | |
<provider>org.hibernate.ejb.HibernatePersistence</provider> | |
<properties> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> | |
<property name="hibernate.hbm2ddl.auto" value="update" /> | |
<property name="hibernate.show_sql" value="true"/> | |
</properties> | |
</persistence-unit> | |
</persistence> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>ToThought-Tutorial-Data</groupId> | |
<artifactId>ToThought-Tutorial-Data</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<properties> | |
<java.source.level>1.7</java.source.level> | |
<spring.version>4.0.1.RELEASE</spring.version> | |
<org.spring.data.version>1.4.3.RELEASE</org.spring.data.version> | |
<com.sun.faces.version>2.2.5</com.sun.faces.version> | |
<org.hibernate.version>4.3.1.Final</org.hibernate.version> | |
<org.primefaces.version>4.0</org.primefaces.version> | |
<junit.version>4.11</junit.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>${junit.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.dbunit</groupId> | |
<artifactId>dbunit</artifactId> | |
<version>2.4.9</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.2.1</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>1.7.5</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-api</artifactId> | |
<version>2.0-beta9</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-slf4j-impl</artifactId> | |
<version>2.0-beta9</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.logging.log4j</groupId> | |
<artifactId>log4j-core</artifactId> | |
<version>2.0-beta9</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>${spring.version}</version> | |
<exclusions> | |
<exclusion> | |
<groupId>commons-logging</groupId> | |
<artifactId>commons-logging</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-web</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-jpa</artifactId> | |
<version>${org.spring.data.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>${spring.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-entitymanager</artifactId> | |
<version>${org.hibernate.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.validation</groupId> | |
<artifactId>validation-api</artifactId> | |
<version>1.1.0.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-validator</artifactId> | |
<version>5.0.2.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>1.4</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>5.1.26</version> | |
</dependency> | |
<dependency> | |
<groupId>joda-time</groupId> | |
<artifactId>joda-time</artifactId> | |
<version>2.3</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cloudfoundry.tothought.entities; | |
import java.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.EnumType; | |
import javax.persistence.Enumerated; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import javax.validation.constraints.NotNull; | |
@Entity | |
@Table(name = "HOL_TEST") | |
public class Test5 implements Serializable { | |
private static final long serialVersionUID = 55741987968716878l; | |
@Id | |
@Column(name = "TEST_ID", insertable = false, updatable = false, nullable = false) | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
@Column(name = "ENUM_COLUMN", nullable = false) | |
@Enumerated(EnumType.STRING) | |
@NotNull | |
private TestEnum testEnum; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public TestEnum getTestEnum() { | |
return testEnum; | |
} | |
public void setTestEnum(TestEnum testEnum) { | |
this.testEnum = testEnum; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cloudfoundry.tothought; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import com.cloudfoundry.tothought.entities.Test5; | |
import com.cloudfoundry.tothought.entities.TestEnum; | |
import com.cloudfoundry.tothought.repositories.TestEnumRepository; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations="classpath:META-INF/application-context.xml") | |
public class Test5EnumTest { | |
@Autowired | |
private TestEnumRepository testEnumRepository; | |
@Test | |
public void testName() throws Exception { | |
Test5 test = new Test5(); | |
test.setTestEnum(TestEnum.A); | |
testEnumRepository.save(test); | |
test = new Test5(); | |
test.setTestEnum(TestEnum.B); | |
testEnumRepository.save(test); | |
System.out.println(testEnumRepository.findAll().size()); | |
System.out.println(testEnumRepository.findByTestEnum(TestEnum.A).size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cloudfoundry.tothought.entities; | |
public enum TestEnum { | |
A, B | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cloudfoundry.tothought.repositories; | |
import java.util.List; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import com.cloudfoundry.tothought.entities.Test5; | |
import com.cloudfoundry.tothought.entities.TestEnum; | |
public interface TestEnumRepository extends JpaRepository<Test5, Long> { | |
public List<Test5> findByTestEnum(TestEnum testEnum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment