@ManyToOne and i@JoinColumn with insertable = false

3,569 views
Skip to first unread message

Edison Azzi

unread,
Jul 17, 2018, 11:24:39 AM7/17/18
to Ebean ORM

Hi Rob,

I'm facing with the following case:

Table:

public class Client implements Serializable {

     @EmbeddedId
    protected ClientPK clientPK;

    ...

    @JoinColumns({
        @JoinColumn(name = "cod_cpny", referencedColumnName = "cod_cpny", insertable = false, updatable = false)
        , @JoinColumn(name = "username", referencedColumnName = "username")})
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Users User;


}

Primary Key:

@Embeddable
public class ClientPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "cod_cpny")
    private int codCompany;

    @Basic(optional = false)
    @Column(name = "cod_client")
    private String codClient;

    ...

}


Users (follow the same pattern):

public class Users implements Serializable {

     @EmbeddedId
    protected UsersPK userPK;
    ...
}

@Embeddable
public class UserPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "cod_cpny")
    private int codCompany;
    @Basic(optional = false)
    @Column(name = "username")
    private String username;
    ...
}

When I try to insert or update a Client, ebean does not include the field "username" in the generated SQL and it fails because this field is not null.
The field "cod_empr" is used as an tennant id (I'm yet not using @TennantId but planning in the future)

I think that is probable that "insertable = false, updatable = false" is causing this issue because if I comment it, it works.

I'm  used to NetBeans 'Persistence->Entity Classes from Database' to reverse engineering the database to JPA
and it genarate that way, but after a while I created my own tool to reverse the database to include additional indexes, columns size,
default values, etc.  to use dbmigration.

Question: Must I follow the JPA pattern with ""insertable = false, updatable = false" (and wait ebean follow JPA)
or is better to not include it and follow Ebean.


att

Edison Azzi

Edison Azzi

unread,
Jul 17, 2018, 11:36:36 AM7/17/18
to Ebean ORM
Correction:

If I remove ""insertable = false, updatable = false" Ebean fails with:

ERROR: column "cod_cpny" specified more than once.

But then "username" is included in the sql sentence.

I think is a bug in Ebean.

Att

Edison Azzi


Rob Bygrave

unread,
Jul 18, 2018, 5:11:56 AM7/18/18
to ebean@googlegroups
Logged as https://github.com/ebean-orm/ebean/issues/1457

In short we either included all columns or excluded all columns (of the @ManyToOne @EmbeddedId).  So the fix is to include/exclude on a per column basis (on those join columns). I'm sure you already knew that but just putting this note here for posterity.

Fixed in 11.18.6

Cheers, Rob.


--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edison Azzi

unread,
Jul 18, 2018, 6:19:23 PM7/18/18
to Ebean ORM
Rob,

  that's it. Now working like a charm.

  I prentend one day publish my tool that reverse engineer a db to java with ebeans annotations and any basemodel, it's almost there but yet incomplete and the source code must be improved.

  Thanks for the speed of the correction.



Rob Bygrave

unread,
Jul 18, 2018, 6:42:56 PM7/18/18
to ebean@googlegroups
>   that's it. Now working like a charm.

Awesome, great to hear !!



> publish my tool that reverse engineer a db to java with ebeans annotations and any basemodel

Ah interesting.  Yes, I think that is a tough problem that (taking into account mappersuperclass etc) ... would be great to see you get it out there some time.


> Thanks for the speed of the correction.

No problem. 



Cheers, Rob.


Reply all
Reply to author
Forward
0 new messages