#1232 ✓resolved
Stéphane Thomas

Unable to load models with a composite key from fixtures

Reported by Stéphane Thomas | November 9th, 2011 @ 09:51 AM | in 1.2.4 (closed)

Framework version: 1.2.x
Platform you're using: Windows 7

Reproduction steps:

Loading models with a composite key from a YAML file with the play.test.Fixtures.loadModels() method throws an Hibernate error 'identifier of an instance of X was altered from Y to Z'.

Details:

In the case we have a table with a composite key:

create table customers (
  customer_code varchar(255) not null,
  ship_to_code varchar(255) not null unique,
  primary key (customer_code, ship_to_code)
);

Note the composite key is made of two fields which are not foreign keys. The corresponding model would be:

@SuppressWarnings("serial")
@Entity
@Table(name = "customers")
@IdClass(CustomersPk.class)
public class Customers extends GenericModel {
  @Id
  @Column(name = "customer_code")
  @NotNull
  public String customerCode;
  
  @Id
  @Column(name = "ship_to_code", unique = true)
  @NotNull
  public String shipToCode;
}

And the composite id class would be:

@SuppressWarnings("serial")
public class CustomersPk implements Serializable {
  public String customerCode;
  public String shipToCode;
  
  @Override
  public int hashCode() {
    final int prime = 12;
    int result = 1;
    
    result = prime * result + ((customerCode == null) ? 0 : customerCode.hashCode());
    result = prime * result + ((shipToCode == null) ? 0 : shipToCode.hashCode());
    
    return result;
  }
  
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    
    if (obj == null)
      return false;
    
    if (getClass() != obj.getClass())
      return false;
    
    CustomersPk other = (CustomersPk) obj;
    
    if (customerCode == null) {
      if (other.customerCode != null)
        return false;
    } else if (!customerCode.equals(other.customerCode))
      return false;
    
    if (shipToCode == null) {
      if (other.shipToCode != null)
        return false;
    } else if (!shipToCode.equals(other.shipToCode))
      return false;
    
    return true;
  }
}

The YAML file could be as simple as this one:

models.Customers(Customers:54899476):
 customerCode: "DOE"
 shipToCode: "123"

models.Customers(Customers:56595609):
 customerCode: "DOE"
 shipToCode: "456"

When calling the play.test.Fixtures.loadModels() method with the name of this YAML file, the following exception is raised:

java.lang.RuntimeException: Cannot load fixture test.yml: org.hibernate.HibernateException: identifier of an instance of models.Customers was altered from models.CustomersPk@345b314 to models.CustomersPk@35f9499
  at play.test.Fixtures.loadModels(Fixtures.java:237)
  at application.Test.mytest(Test.java:96)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
  at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:105)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:55)
  at org.junit.runners.Suite.runChild(Suite.java:128)
  at org.junit.runners.Suite.runChild(Suite.java:24)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
  at play.test.TestEngine.run(TestEngine.java:112)
  at controllers.TestRunner.run(TestRunner.java:66)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:550)
  at play.mvc.ActionInvoker.invoke(ActionInvoker.java:504)
  at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:480)
  at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:475)
  at play.mvc.ActionInvoker.invoke(ActionInvoker.java:163)
  at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:257)
  at play.Invoker$Invocation.run(Invoker.java:278)
  at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:235)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662)
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of models.Customers was altered from models.CustomersPk@345b314 to models.CustomersPk@35f9499
  at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
  at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
  at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
  at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:798)
  at play.db.jpa.JPABase._save(JPABase.java:35)
  at play.test.Fixtures.loadModels(Fixtures.java:215)
  ... 53 more
Caused by: org.hibernate.HibernateException: identifier of an instance of models.Customers was altered from models.CustomersPk@345b314 to models.CustomersPk@35f9499
  at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:85)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
  at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:240)
  at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
  ... 55 more

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2>Play framework</h2>

Play makes it easier to build Web applications with Java. It is a clean alternative to bloated Enterprise Java stacks. It focuses on developer productivity and targets RESTful architectures. Learn more on the <a href="http://www.playframework.org">http://www.playframework.org</a> website.<br><br>

<h2>Source code is hosted on github</h2>Check out our repository at <a href="http://github.com/playframework/play">http://github.com/playframework/play</a><br><br>

<h2>Contributing, creating a patch</h2> Please read the <a href="http://play.lighthouseapp.com/projects/57987/contributor-guide">contributor guide</a><br><br>

<h2>Reporting Security Vulnerabilities</h2> Since all bug reports are public, please report any security vulnerability directly to <em>guillaume dot bort at gmail dot com</em>.<br><br>

<h2>Creating a bug report</h2> Bug reports are incredibly helpful, so take time to report bugs and request features in our ticket tracker. We’re always grateful for patches to Play’s code. Indeed, bug reports with attached patches will get fixed far quickly than those without any.<br><br>

Please include as much relevant information as possible including the exact framework version you're using and a code snippet that reproduces the problem.<br><br>

Don't have too much expectations. Unless the bug is really a serious "everything is broken" thing, you're creating a ticket to start a discussion. Having a patch (or a branch on Github we can pull from) is better, but then again we'll only pull high quality branches that make sense to be in the core of Play.

Pages