Skip to content

Tutorial 1: Getting Started

Kumar Rohit edited this page Oct 2, 2015 · 3 revisions

Getting Started

Setting up

You will need the following:

  1. JDK 1.5+
  2. Random-JPA jar in your classpath see - random-JPA latest jar or Maven

Simple Test

Suppose you have the following class Person that you would like to generate test data.

Entity

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    private Long id;

    @Column(name = "NAME")
    private String name;
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Creating StaticMetaModel

@StaticMetamodel(Person.class)
public class Person_ {
    public static volatile SingularAttribute<Person, Long> id;
    public static volatile SingularAttribute<Person, String> name;
}

To test it you'll need the following JUnit test

@Test
public void testRandomPersonIsCreated() throws Exception {
    final JPAContext jpaContext = JPAContextFactory
        .newInstance(Database.ORACLE, entityManager)
        .create();        
    final Plan plan = Plan.create();
    final CreationPlan creationPlan = jpaContext.create(plan.add(Entity.of(Person.class)));
    final ResultMap persisted = jpaContext.persist(creationPlan);
    persisted.print(new Printer() {
        @Override
        public void print(String s) {
            System.out.println(s);
        }
    });
    
    final Person person = persisted.get(Person.class);
    System.out.println("person id: " + person.getId());
    System.out.println("person name: " + person.getName());
}

It would print on the console

└── *ROOT*
    └── com.github.kuros.sample.entity.Person|0 [id: 324]
person id: 324 **(id of the persisted person object)**
person name: jklaksd **(some random value)**

It is advisable to create a singleton class to construct JPAContext

Clone this wiki locally