Toggle navigation OptaPlanner logo
  • Home
  • Download
  • Learn
    • Documentation
    • Videos
    • Slides
    • Training
    • Use cases
    • Testimonials and Case Studies
  • Get Help
  • Source
  • Team
  • Services
  • @OptaPlanner Fb
Fork me on GitHub
  • What is the fastest Garbage Collector in Java 8?
  • How lucky are your random seeds?

Integrating JPA Hibernate with OptaPlanner

Wed 23 September 2015

Avatar Geoffrey De Smet

Geoffrey De Smet


Twitter LinkedIn GitHub

OptaPlanner lead

We’ve been improving the integration of OptaPlanner with the rest of JEE, so it’s easier to build end user applications that just work. Let’s take a look at the improved JPA Hibernate integration.

The basics

Both JPA Hibernate and OptaPlanner work on POJO’s (Plain Old Java Objects), so just add some JPA annotations on your domain objects to persist them with JPA Hibernate and add some OptaPlanner annotations to solve your optimization problem with OptaPlanner.

On each problem fact class, there are usually only JPA annotations:

@Entity // JPA annotation
public class Computer {

    private int cpuPower;
    private int memory;
    private int networkBandwidth;
    private int cost;

    ...
}

On each planning entity class, there are both JPA and OptaPlanner annotations:

@PlanningEntity // OptaPlanner annotation
@Entity // JPA annotation
public class Process {

    private int requiredCpuPower;
    private int requiredMemory;
    private int requiredNetworkBandwidth;

    @PlanningVariable(...) // OptaPlanner annotation
    @ManyToOne() // JPA annotation
    private Computer computer;

    ...
}

Don’t confuse a JPA entity (anything object that gets persisted in the database) with an OptaPlanner planning entity (an object that gets changed by OptaPlanner during solving).

Persisting a score

By default, JPA Hibernate will put a Score in a BLOB column through Java serialization. This is undesirable because it prevents using the score in a JPA-QL query. Furthermore, it triggers database issues when upgrading the OptaPlanner version.

Therefore, OptaPlanner 6.4.0.Beta1 has a new jar optaplanner-persistence-jpa that contains a Hibernate type for each score type. Use it like this:

@PlanningSolution // OptaPlanner annotation
@Entity // JPA annotation
@TypeDef(defaultForType = HardSoftScore.class, typeClass = HardSoftScoreHibernateType.class) // Hibernate annotation
public class CloudBalance implements Solution<HardSoftScore> {

    @Columns(columns = {@Column(name = "hardScore"), @Column(name = "softScore")}) // JPA annotation
    private HardSoftScore score;

    ...
}

This puts the HardSoftScore into 2 INTEGER columns, instead of a BLOB column. The OptaPlanner reference manual contain more information on how to deal with BigDecimal and/or bendable scores properly.

Cloning pitfall

In a JPA model it’s common that the problem facts reference the planning solution, which can corrupt planning cloning (if the default planning cloner is used).

To overcome this, simply annotate the problem fact classes that reference the planning solution or a planning entity with a @DeepPlanningClone annotation:

@DeepPlanningClone // OptaPlanner annotation: Force the default planning cloner to planning clone this class too
@Entity // JPA annotation
public class Computer {

    @ManyToOne
    private CloudBalance cloudBalance;

    ...
}

This way, the Computer class is planning cloned too and the clone’s cloudBalance field will point to the CloudBalance clone.

Conclusion

You can use the same domain classes for JPA Hibernate and OptaPlanner, there is no need duplicate your domain!


Comments Permalink
 tagged as integration

Comments

Visit our forum to comment
  • What is the fastest Garbage Collector in Java 8?
  • How lucky are your random seeds?
Atom News feed
Don't want to miss a single blog post?
Follow us on
  • T
  • Fb
Blog archive
Latest blog posts
  • Exploring the new OptaWeb Employee Rostering backend
    Tue 22 October 2019
     Julian Cui
  • Mechanic scheduling (part 3) - Simulation and load testing
    Thu 9 May 2019
     Radovan Synek
  • Mechanic scheduling (part 2) - Architecture and integration
    Thu 9 May 2019
     Musa Talluzi
  • Mechanic scheduling (part 1) - Can OptaPlanner keep up with a keynote audience of thousands?
    Thu 9 May 2019
     Geoffrey De Smet
  • KIE Server OptaPlanner Task Assignment
    Wed 13 March 2019
     Musa Talluzi
  • How much faster is Java 11?
    Thu 17 January 2019
     Radovan Synek
  • Red Hat Mobile Portfolio Truck dodges storms while keeping Sales happy with OptaPlanner
    Mon 19 November 2018
     Sudheer Chekka
Archive
Latest releases
  • 7.30.0.Final released
    Wed 27 November 2019
Upcoming events
  • Red Hat Summit
    San Francisco, CA, USA - Mon 27 April 2020
Add event / Archive

KIE projects

  • Drools rule engine
  • OptaPlanner constraint solver
  • jBPM workflow engine

Community

  • Blog
  • Get Help
  • Team
  • Governance
  • Academic research

Code

  • Build from source
  • Submit a bug
  • License (Apache)
  • Release notes
  • Upgrade recipes
Sponsored by
Red Hat
More coder content at
Red Hat Developers
© Copyright 2006-2019, Red Hat, Inc. or third-party contributors - Privacy statement - Terms of use - Website info