Question

I'm trying to make a many to many relationship using EBean in Play2 and I have an issue where EBean trows an error saying my class is not registered. Heres my mapping classes:

@Entity
public class Booking extends Model {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;
   //... other fields
}
@Entity
public class Store extends Model {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;
   //... other fields
}

@Embeddable
public class ComissionId {
   @ManyToOne(targetEntity = Booking.class, optional = false)
   @PrimaryKeyJoinColumn(name = "booking_id", referencedColumnName = "id")
   private Booking booking;

   @ManyToOne(targetEntity = Store.class, optional = false)
   @PrimaryKeyJoinColumn(name = "store_id", referencedColumnName = "id")
   private Store store;
}

@Entity
@AssociationOverrides({
    @AssociationOverride(name = "id.booking", joinColumns = @JoinColumn(name = "booking_id")),
    @AssociationOverride(name = "id.store", joinColumns = @JoinColumn(name = "store_id"))
 })
 public class StoreComission extends Model {
    @EmbeddedId
    private ComissionId id;
    @Column(nullable = false)
    private double value;
    @Column(nullable = false)
    private Date date;
}

The error:

java.lang.RuntimeException: 
  Error reading annotations for models.ids.ComissionId
  Caused by: java.lang.RuntimeException: 
  Error with association to [class models.Booking] from [models.ids.ComissionId.booking]. 
  Is class models.Booking registered?

In my application.conf I've put ebean.default="models.*" so all this classes should be registered right? (I've tried to move the ComissionId from the package models.ids to models, but the same error ocurred)

Was it helpful?

Solution

You have this error because of @ManyToOne annotations inside your ComissionId class. To make this code work you have to move these relations to StoreComission class. In ComissionId class you should leave only identifiers. In StoreComission class @ManyToOne relation are mapped to the same columns as fields from composite key. But they have attributes 'insertable' and 'updateable' set to false to prevent column duplication.
Here is corrected working code for above scenario:

StoreComission class:

@Entity
public class StoreComission extends Model {

    public StoreComission() {
        id = new ComissionId();
    }

    @EmbeddedId
    private ComissionId id;

    @Column(nullable = false)
    public double value;

    @Column(nullable = false)
    public Date date;

    @ManyToOne
    @JoinColumn(name = "booking_id", insertable = false, updatable = false)
    private Booking booking;

    @ManyToOne
    @JoinColumn(name = "store_id", insertable = false, updatable = false)
    private Store store;

    public void setBooking(Booking aBooking) {
        booking = aBooking;
        id.booking_id = aBooking.id;
    }

    public Booking getBooking() {
        return booking;
    }

    public void setStore(Store aStore) {
        store = aStore;
        id.store_id = aStore.id;
    }

    public Store getStore() {
        return store;
    }
}

ComissionId class:

@Embeddable
public class ComissionId {

    public int booking_id;

    public int store_id;

    @Override
    public int hashCode() {
        return booking_id + store_id;
    }

    @Override
    public boolean equals(final Object obj) {
        return super.equals(obj);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top