This is the 14th day of my participation in Gwen Challenge

JPA supports

To add JPA support for Activiti in Spring Boot, add the following dependencies:

<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-spring-boot-starter-jpa</artifactId>
	<version>${activiti.version}</version>
</dependency>
Copy the code

This adds the Spring configuration and beans to use JPA. By default, the JPA provider will be Hibernate.

Let’s create a simple entity class:

@Entity
class Person {

    @Id
    @GeneratedValue
    private Long id;

    private String username;

    private String firstName;

    private String lastName;

    private Date birthDate;

    public Person(a) {}public Person(String username, String firstName, String lastName, Date birthDate) {
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
    }

    public Long getId(a) {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername(a) {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFirstName(a) {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName(a) {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate(a) {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate; }}Copy the code

By default, tables are not created automatically when an in-memory database is not in use. Create a file application.properties on the classpath and add the following properties:

Spring. Jpa. Hibernate. DDL - auto = updateCopy the code

Add the following classes:

public interface PersonRepository extends JpaRepository<Person.Long> {

    Person findByUsername(String username);

}
Copy the code

This is a Spring repository that provides CRUD out of the box. We added a method to find Person by user name. Spring does this automatically based on the convention (that is, the property name used).

We now further enhance our services by:

By adding @Transactional to the class. Please note that by adding the above JPA dependency, we used before DataSourceTransactionManager JpaTransactionManager automatically change out now.

The startProcess now gets the user name of the transferee, which is used to find the person, using the person JPA object as the process variable of the process instance.

Added methods to create virtual users. This is used in CommandLineRunner to populate the database.

@Service
@Transactional
public class MyService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private PersonRepository personRepository;

    public void startProcess(String assignee) {

        Person person = personRepository.findByUsername(assignee);

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("person", person);
        runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);
    }

    public List<Task> getTasks(String assignee) {
        return taskService.createTaskQuery().taskAssignee(assignee).list();
    }

    public void createDemoUsers(a) {
		 if (personRepository.findAll().size() == 0) {
            personRepository.save(new Person("jbarrez"."Joram"."Barrez".new Date()));
            personRepository.save(new Person("trademakers"."Tijs"."Rademakers".newDate())); }}}Copy the code

CommandLineRunner now looks like:

@Bean
public CommandLineRunner init(final MyService myService) {

	return new CommandLineRunner() {
    	public void run(String... strings) throws Exception { myService.createDemoUsers(); }}; }Copy the code

RestController has also changed slightly to incorporate the above changes (just to show the new method), and the HTTP POST now has a body containing the transferee user name:

@RestControllerPublic MyRestController {@autoconnect private MyService MyService;@RequestMapping(value="/process", method= RequestMethod.POST)
    public void startProcessInstance(@RequestBody StartProcessRepresentation startProcessRepresentation) { myService.startProcess(startProcessRepresentation.getAssignee()); }... Static class StartProcessRepresentation {private string the transferee; The public string getAssignee() {returns the assignee; } public invalid setAssignee (string assignee) {this.assignee = ignee; }}Copy the code

Finally, to try the Spring-JPA-Activiti integration, we assign tasks using the ID of the Person JPA object in the process definition:

<userTask id="theTask" name="my task" activiti:assignee="${person.id}"/>
Copy the code

We can now start a new process instance, providing the user name in the body of the POST:

curl -H "Content-Type: application/json" -d '{"assignee" : "jbarrez"}' http://localhost:8080/process
Copy the code

Now use the staff ID to get the task list:

curl http://localhost:8080/tasks? Assignee =1 [{" ID ":"12505","name":" my task "}]Copy the code