Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANNER-444 Extract AbstractScoreHibernateType
- Loading branch information
Showing
5 changed files
with
186 additions
and
123 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
.../src/main/java/org/optaplanner/persistence/jpa/impl/score/AbstractScoreHibernateType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2015 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.optaplanner.persistence.jpa.impl.score; | ||
|
||
import java.io.Serializable; | ||
|
||
import org.hibernate.engine.spi.SessionImplementor; | ||
import org.hibernate.usertype.CompositeUserType; | ||
|
||
/** | ||
* This class is Hibernate specific, because JPA 2.1's @Converter currently | ||
* cannot handle 1 class mapping to multiple SQL columns. | ||
*/ | ||
public abstract class AbstractScoreHibernateType implements CompositeUserType { | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Object deepCopy(Object value) { | ||
return value; // Score is immutable | ||
} | ||
|
||
@Override | ||
public Object replace(Object original, Object target, SessionImplementor session, Object owner) { | ||
return original; // Score is immutable | ||
} | ||
|
||
@Override | ||
public void setPropertyValue(Object component, int property, Object value) { | ||
throw new UnsupportedOperationException("A Score is immutable."); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object a, Object b) { | ||
if (a == b) { | ||
return true; | ||
} else if (a == null || b == null) { | ||
return false; | ||
} | ||
return a.equals(b); | ||
} | ||
|
||
@Override | ||
public int hashCode(Object o) { | ||
if (o == null) { | ||
return 0; | ||
} | ||
return o.hashCode(); | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(Object value, SessionImplementor session) { | ||
return (Serializable) value; | ||
} | ||
|
||
@Override | ||
public Object assemble(Serializable cached, SessionImplementor session, Object owner) { | ||
return cached; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
.../test/java/org/optaplanner/persistence/jpa/impl/score/AbstractScoreHibernateTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2015 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.optaplanner.persistence.jpa.impl.score; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
import javax.persistence.Persistence; | ||
import javax.persistence.Transient; | ||
|
||
import org.junit.BeforeClass; | ||
import org.optaplanner.core.api.score.Score; | ||
import org.optaplanner.persistence.jpa.impl.score.buildin.hardsoft.HardSoftScoreHibernateTypeTest; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public abstract class AbstractScoreHibernateTypeTest { | ||
|
||
protected static EntityManagerFactory entityManagerFactory; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
entityManagerFactory = Persistence.createEntityManagerFactory("optaplanner-persistence-jpa-test"); | ||
} | ||
|
||
protected <S extends Score, E extends AbstractTestJpaEntity<S>> Long persistAndAssert(E jpaEntity) { | ||
EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
EntityTransaction transaction = entityManager.getTransaction(); | ||
transaction.begin(); | ||
entityManager.persist(jpaEntity); | ||
transaction.commit(); | ||
Long id = jpaEntity.getId(); | ||
assertNotNull(id); | ||
return id; | ||
} | ||
|
||
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAssertAndChangeScore( | ||
Class<E> jpaEntityClass, Long id, S oldScore, S newScore) { | ||
EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
EntityTransaction transaction = entityManager.getTransaction(); | ||
transaction.begin(); | ||
E jpaEntity = entityManager.find(jpaEntityClass, id); | ||
assertEquals(oldScore, jpaEntity.getScore()); | ||
jpaEntity.setScore(newScore); | ||
jpaEntity = entityManager.merge(jpaEntity); | ||
transaction.commit(); | ||
} | ||
|
||
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAndAssert( | ||
Class<E> jpaEntityClass, Long id, S score) { | ||
EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
EntityTransaction transaction = entityManager.getTransaction(); | ||
transaction.begin(); | ||
E jpaEntity = entityManager.find(jpaEntityClass, id); | ||
assertEquals(score, jpaEntity.getScore()); | ||
transaction.commit(); | ||
} | ||
|
||
@MappedSuperclass | ||
protected static abstract class AbstractTestJpaEntity<S extends Score> { | ||
|
||
protected Long id; | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@Transient | ||
public abstract S getScore(); | ||
public abstract void setScore(S score); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters