play framework 2.0.4, Java : compound primary key declaration and assignment

Using Ebean as ORM, I have the following Model class :

@Entity
@Table(name = "update_proposition")
public class UpdateProposition extends Model {

    @EmbeddedId
    public UpdatePropositionKey id;
    public String               fieldName;
    public String               oldValue;
    public String               newValue;

    @Embeddable
    public class UpdatePropositionKey implements Serializable {

        @ManyToOne
        @JoinColumn(name = "update_request")
        public UpdateRequest updateRequest;
        public Date          date;
        @Id
        public int           serial;

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(final Object obj) {
            return super.equals(obj);
        }
    }
}

My goal is to map a table with a primary key compound by a foreign key, a date and an auto-incremented serial number. For the instance, this Model throws a RuntimeException: Error reading annotations. How can I implement my use case ?

Once this problem solved, how to assign the date and the foreign key ? Will a pattern like updateProposition.id.date = Calendar.getInstance().getTime() work fine ?

Thanks for your help.

Answers


I found solution to this problem. Your error was because of @ManyToOne annotation in UpdatePropositionKey class. I moved this relation to UpdateProposition class leaving only UpdateRequest.id. So now there are two mappings from UpdateProposition class to UpdateRequest class. one is througt composite key and second through @ManyToOne relation. Both mappings use the same column. Additionally @JoinColumn annotation has attributes 'updatable and 'insertable' set to false. After these all changescode looks as follows:

@Entity
@Table(name = "update_proposition")
public class UpdateProposition extends Model {

    public UpdateProposition(int aSerial, Date aDate) {
        id = new UpdatePropositionKey();
        id.serial = aSerial;
        id.date = aDate;
    }

    @EmbeddedId
    private UpdatePropositionKey id;

    @ManyToOne
    @JoinColumn(name = "update_request_id", insertable = false, updatable = false)
    private UpdateRequest updateRequest;

    public String fieldName;
    public String oldValue;
    public String newValue;

    public void setUpdateRequest(UpdateRequest aUpdateRequest) {
        updateRequest = aUpdateRequest;
        id.updateRequest_id = aUpdateRequest.id;
    }

    public UpdateRequest getUpdateRequest() {
        return updateRequest;
    }
}

@Embeddable
public class UpdatePropositionKey implements Serializable {

    @Id
    public int serial;

    public Date date;

    public int updateRequest_id;

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(final Object obj) {
        return super.equals(obj);
    }
}

Need Your Help

How to detect undefined in ngSwitch and ngSwitchWhen? (Angular 2)

typescript angular

How to detect undefined in ngSwitch and ngSwitchWhen?

Creating an array that counts up to a given number

java

I need help creating an array that counts up to a given number. The output should look something like this: