简体   繁体   English

使用Spring Data JPA进行简单测试

[英]simple test using Spring Data JPA

I'm a beginner in Spring Data JPA, attracted by its feature where no Impl is required. 我是Spring Data JPA的初学者,其吸引力在于不需要Impl的功能。 But I'm have some trouble with a simple startup test. 但是我在进行简单的启动测试时遇到了一些麻烦。 Basically I just want to create two Entities, Person and Pet, where Person can have a 1-to-many relationship to Pets. 基本上,我只想创建两个实体,“人”和“宠物”,其中“人”可以与“宠物”建立一对多关系。 I'd like to create a few persons and pets and test if they are stored into DB. 我想创建一些人和宠物,并测试它们是否存储在数据库中。 But the problem is, I have no idea how this can be implemented in a main() method. 但是问题是,我不知道如何在main()方法中实现这一点。 I've checked several tutorials on spring data jpa but still no concrete progress (forgive my negligence) 我已经查看了有关spring data jpa的一些教程,但仍然没有任何具体进展(请原谅我的疏忽)

Here the classes go(getters,setters and imports are omitted): 这里是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");

    }

}

I simply don't know what will be stored into database and when will it happen. 我只是不知道什么将存储到数据库中以及何时会发生。 I have configured some beans in my applicationContext.xml, and added scanning on repository. 我在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>

And I have a persistence unit pointing to my local derby DB. 我有一个指向我本地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>

I've spent hours checking out different tutorials but they don't really meet my need (maybe simply because my questions are too idiot but I'm just doing a HelloWorld example). 我已经花了数小时来检查不同的教程,但是它们并不能真正满足我的需要(可能只是因为我的问题太白痴了,而我只是在做一个HelloWorld示例)。 Appreciated if you could tell me what is still missing. 如果您能告诉我还缺少什么,我将不胜感激。 I'm using Eclipselink for JPA. 我正在将Eclipselink用于JPA。 Maven as repository management. Maven作为存储库管理。

Take a look at this sample. 看一下这个样本。 It uses Spring Boot and shows how to use Spring Data JPA to store and retrieve entities from the database. 它使用Spring Boot并展示了如何使用Spring Data JPA来存储和检索数据库中的实体。 https://spring.io/guides/gs/accessing-data-jpa/ Hope it helps. https://spring.io/guides/gs/accessing-data-jpa/希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM