Re: [ebean] OneToOne key generation

69 views
Skip to first unread message

Rob Bygrave

unread,
May 12, 2013, 5:08:58 PM5/12/13
to eb...@googlegroups.com
>> So is it possible to have the 'user_id' of the child Settings object 'automagically' get the same 'user_id' of it's newly created parent User?

The same Primary Key value?  No, there is no feature to support that.

>> would converting to a SecondaryTable strategy be simpler/"better"?

Generally I expect a normal 1 to 1 relationship ... with a foreign key that also has a unique constraint on it.


On 11 May 2013 06:33, Brannon Smith <gbs...@gmail.com> wrote:
So we have a MySQL 'users' table with an Auto-Increment PK called 'user_id' mapped to 'User' class.

We would like to add a 'user_settings' table mapped to 'Setting' class in a OneToOne relationship to 'User' with a shared PK, also called 'user_id' (non A-I this time) on that table.

So is it possible to have the 'user_id' of the child Settings object 'automagically' get the same 'user_id' of it's newly created parent User? Or does this require manual intervention?

IIRC, Hibernate had a special generator that could take care of this. 



If this is not possible, would converting to a SecondaryTable strategy be simpler/"better"?

If all else fails, I suppose we could just fold the settings right into the user, but I was hoping to keep those separate.

-GBS


--
 
---
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/groups/opt_out.
 
 

Brannon Smith

unread,
May 12, 2013, 10:18:58 PM5/12/13
to eb...@googlegroups.com
I guess I see a shared PK as no less normal than a unique FK in such a case. ;-)

But say I could get SecondaryTable to work (more on that in a moment) wouldn't that automatically set a shared PK on the second table? In other words, wouldn't the behavior I asked about that does not exist with OneToOne, work in the context of SecondaryTable?

After my initial post, I tried to reimplement with SecondaryTable:

@Entity
@Table(name = "users")
@SecondaryTable(name = "user_settings")
public class User extends Model
{
   @Id
   @Column(name = "user_id")
   public Long id;

   @Column(unique = true)
   public String username;

   @Column(table="user_settings")
   private String timeZone;

   // [...other stuff...]
}

But I get this stack trace, even though both the users and user_settings table exist each with a user_id column (auto-inc on users):

[error] c.a.e.s.d.BeanDescriptorManager - Error in deployment
java.lang.NullPointerException: null
at com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor.findJoinToTable(DeployBeanDescriptor.java:899) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.secondaryPropsJoins(BeanDescriptorManager.java:658) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readEntityRelationships(BeanDescriptorManager.java:628) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.deploy(BeanDescriptorManager.java:256) ~[avaje-ebeanorm-server.jar:na]
        [snip]

Have I left out some necessary config?

-GBS
Reply all
Reply to author
Forward
0 new messages