Close

Spring Data JPA - XML Data Repository Populator

[Last Updated: Aug 28, 2018]

In following example we will see how to import XML Data to populate JPA entities. (Check out last tutorial to import JSON data).

Example

Entity

@Entity
@XmlRootElement
public class Employee {
    private @Id
    @GeneratedValue
    Long id;
    private String name;
    private String dept;
    .............
}

JavaConfig

@EnableJpaRepositories
@ComponentScan
@Configuration
public class AppConfig {

    @Bean
    public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(Employee.class);
        UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
        factory.setUnmarshaller(marshaller);
        factory.setResources(new Resource[]{new ClassPathResource("employee-data.xml"),
                new ClassPathResource("employee-data2.xml")});
        return factory;
    }

    @Bean
    EntityManagerFactory entityManagerFactory() {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("example-unit");
        return emf;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }
}

XML data

src/main/resources/employee-data.xml

<employee>
    <dept>IT</dept>
    <name>Lara</name>
</employee>

src/main/resources/employee-data2.xml

<employee>
    <dept>Admin</dept>
    <name>Wayne</name>
</employee>

Repository

public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}

Example Client

@Component
public class ExampleClient {

    @Autowired
    private EmployeeRepository repo;

    public void run() {
        System.out.println(" -- finding all employees --");
        Iterable<Employee> all = repo.findAll();
        all.forEach(System.out::println);
    }
}

Main class

public class ExampleMain {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);
        ExampleClient exampleClient = context.getBean(ExampleClient.class);
        exampleClient.run();
        EntityManagerFactory emf = context.getBean(EntityManagerFactory.class);
        emf.close();
    }
}
 -- finding all employees --
Employee{id=1, name='Lara', dept='IT'}
Employee{id=2, name='Wayne', dept='Admin'}

Example Project

Dependencies and Technologies Used:

  • spring-data-jpa 2.0.8.RELEASE: Spring Data module for JPA repositories.
    Uses org.springframework:spring-context version 5.0.7.RELEASE
  • hibernate-core 5.3.1.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • h2 1.4.197: H2 Database Engine.
  • spring-oxm 5.0.7.RELEASE: Spring Object/XML Marshalling.
  • JDK 1.8
  • Maven 3.5.4

Importing JSON Data to populate JPA entities. Select All Download
  • spring-data-repository-populator-xml-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • AppConfig.java
          • resources
            • META-INF

    See Also