Toggle navigation OptaPlanner logo
  • Home
  • Download
  • Learn
    • Documentation
    • Videos
    • Slides
    • Training
    • Use cases
    • Compatibility
    • Testimonials and case studies
  • Get help
  • Source
  • Team
  • Services
  • Star
  • @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 release
  • 8.1.0.Final released
    Fri 15 January 2021
Upcoming events
  • KIE Live
    Worldwide - Tue 19 January 2021
    • OptaPlanner Shadow Variables for the Vehicle Routing Problem and Task Assignment by Geoffrey De Smet, Karina Varela, Alex Porcelli
  • Javaland
    Worldwide - Tue 16 March 2021
    • AI on Quarkus: I love it when an OptaPlan comes together by Geoffrey De Smet
Add event / Archive
Latest blog posts
  • Solve the facility location problem
    Fri 9 October 2020
     Jiří Locker
  • OptaPlanner Week 2020 recordings
    Mon 7 September 2020
     Geoffrey De Smet
  • Let’s OptaPlan your jBPM tasks (part 1) - Integrating the two worlds
    Fri 3 July 2020
     Walter Medvedeo
  • AI versus Covid-19: How Java helps nurses and doctors in this fight
    Fri 8 May 2020
     Christopher Chianelli
  • Workflow processes with AI scheduling
    Tue 5 May 2020
     Christopher Chianelli
  • Constraint Streams - Modern Java constraints without the Drools Rule Language
    Tue 7 April 2020
     Geoffrey De Smet
  • How to plan (and optimize) a Secret Santa
    Wed 18 December 2019
     Christopher Chianelli
Blog archive
Latest videos
  • YT Shadow variables
    Tue 19 January 2021
     Geoffrey De Smet
  • YT Domain modeling and design patterns
    Tue 17 November 2020
     Geoffrey De Smet
  • YT Quarkus insights: AI constraint solving
    Tue 20 October 2020
     Geoffrey De Smet
  • YT AI in kotlin
    Wed 23 September 2020
     Geoffrey De Smet
  • YT Planning agility: continuous planning, real-time planning and more
    Thu 3 September 2020
     Geoffrey De Smet
  • YT Quarkus and OptaPlanner: create a school timetable application
    Thu 3 September 2020
     Radovan Synek
  • YT Business use cases and the impact of OptaPlanner
    Thu 3 September 2020
     Satish Kale
Video 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-2.0)
  • Release notes
  • Upgrade recipes
Sponsored by
Red Hat
More coder content at
Red Hat Developers
© Copyright 2006-2021, Red Hat, Inc. or third-party contributors - Privacy statement - Terms of use - Website info