我是Spring Data JPA的初学者,被它不需要Impl的特性所吸引。但是我在一个简单的启动测试上遇到了一些麻烦。基本上我只想创建两个实体,Person和Pet,其中Person和Pets可以有一对多的关系。我想创建一些人和宠物,并测试他们是否存储到数据库中。但问题是,我不知道如何在main()方法中实现这一点。我已经检查了几个关于spring data jpa的教程,但仍然没有具体的进展(原谅我的疏忽)
下面是类(省略了getter、setter和imports):
@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数据库。
<?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的例子)。如果你能告诉我还缺少什么,我将不胜感激。我对JPA使用Eclipselink。Maven作为存储库管理
看一下这个示例。它使用Spring Boot并展示了如何使用Spring Data JPA从数据库中存储和检索实体。https://spring.io/guides/gs/accessing-data-jpa/