簡體   English   中英

使用Spring Data JPA進行簡單測試

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM