OptaPlanner logo
  • Download
  • Learn
    • Documentation
    • Videos

    • Use cases
    • Compatibility
    • Testimonials and case studies
  • Get help
  • Blog
  • Source
  • Team
  • Services
  • Star
  • T
  • L
  • F
  • YT
Fork me on GitHub

Integrating JPA Hibernate with OptaPlanner

Wed 23 September 2015
Avatar Geoffrey De Smet
Geoffrey De Smet

Twitter LinkedIn GitHub

OptaPlanner creator

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 POJOs (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!


Permalink
 tagged as integration

Comments

Visit our forum to comment
AtomNews feed
Don’t want to miss a single blog post?
Follow us on
  • T
  • L
  • F
Blog archive
Latest release
  • 8.35.0.Final released
    Fri 3 March 2023
Upcoming events
    Add event / Archive
Latest blog posts
  • OptaPlanner 9 is coming
    Tue 21 February 2023
    Lukáš Petrovický
  • Farewell - a new lead
    Tue 15 November 2022
    Geoffrey De Smet
  • Run OptaPlanner workloads on OpenShift, part II
    Wed 9 November 2022
    Radovan Synek
  • Bavet - A faster score engine for OptaPlanner
    Tue 6 September 2022
    Geoffrey De Smet
  • Run OptaPlanner workloads on OpenShift, part I.
    Thu 9 June 2022
    Radovan Synek
  • OptaPlanner deprecates score DRL
    Thu 26 May 2022
    Lukáš Petrovický
  • Real-time planning meets SolverManager
    Mon 7 March 2022
    Radovan Synek
  • Blog archive
Latest videos
  • The Vehicle Routing Problem
    Fri 23 September 2022
    Geoffrey De Smet
  • Introduction to OptaPlanner AI constraint solver
    Thu 25 August 2022
    Anna Dupliak
  • On schedule: Artificial Intelligence plans that meet expectations
    Sat 23 July 2022
    Geoffrey De Smet
  • Host your OptaPlanner app on OpenShift (Kubernetes)
    Mon 7 February 2022
    Geoffrey De Smet
  • OptaPlanner - A fast, easy-to-use, open source AI constraint solver for software developers
    Mon 31 January 2022
  • Order picking planning with OptaPlanner
    Fri 31 December 2021
    Anna Dupliak
  • AI lesson scheduling on Quarkus with OptaPlanner
    Thu 18 November 2021
    Geoffrey De Smet
  • Video archive

OptaPlanner is open. All dependencies of this project are available under the Apache Software License 2.0 or a compatible license. OptaPlanner is trademarked.

This website was built with JBake and is open source.

Community

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

Code

  • Build from source
  • Issue tracker
  • Release notes
  • Upgrade recipes
  • Logo and branding
CC by 3.0 | Privacy Policy
Sponsored by Red Hat