[英]simple test using Spring Data JPA
我是Spring Data JPA的初学者,其吸引力在于不需要Impl的功能。 但是我在进行简单的启动测试时遇到了一些麻烦。 基本上,我只想创建两个实体,“人”和“宠物”,其中“人”可以与“宠物”建立一对多关系。 我想创建一些人和宠物,并测试它们是否存储在数据库中。 但是问题是,我不知道如何在main()方法中实现这一点。 我已经查看了有关spring data jpa的一些教程,但仍然没有任何具体进展(请原谅我的疏忽)
这里是go类(省略了getters,setters和import):
@Entity
public class Person implements Serializable {
@Basic
private String name;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Version
private int version;
@OneToMany(targetEntity = Pet.class)
private Collection<Pet> pet;
}
@Entity
public class Pet implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Basic
private String type;
@Version
private int version;
public Pet() {
}
}
public interface PersonRepository extends CrudRepository<Person,Long>{
Person findByPet(Pet p);
}
public class SimpleTest {
public static void main(String[] args) throws BeansException {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person tom=(Person)context.getBean("tom");
}
}
我只是不知道什么将存储到数据库中以及何时会发生。 我在applicationContext.xml中配置了一些bean,并在存储库上添加了扫描。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="simpletest" />
<bean id="dog" class="Pet">
</bean>
<bean id="cat" class="Pet">
</bean>
<bean id="tom" class="Person">
<property name="name">
<value>tom</value>
</property>
<property name="pet">
<list>
<value>
<ref bean="dog"/>
</value>
</list>
</property>
</bean>
</beans>
我有一个指向我本地derby DB的持久性单元。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="SimpleTestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/GEAH"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
我已经花了数小时来检查不同的教程,但是它们并不能真正满足我的需要(可能只是因为我的问题太白痴了,而我只是在做一个HelloWorld示例)。 如果您能告诉我还缺少什么,我将不胜感激。 我正在将Eclipselink用于JPA。 Maven作为存储库管理。
看一下这个样本。 它使用Spring Boot并展示了如何使用Spring Data JPA来存储和检索数据库中的实体。 https://spring.io/guides/gs/accessing-data-jpa/希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.