Can someone give me the steps to make a foreign key on my classes please.

30 views
Skip to first unread message

James Goodman

unread,
Apr 13, 2017, 7:05:57 PM4/13/17
to Play Framework
I have two classes;

# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions

# --- !Ups

create table customer (
id integer auto_increment not null,
first_name varchar(255),
last_name varchar(255),
phone_number varchar(255),
email varchar(255),
address1 varchar(255),
address2 varchar(255),
suburb varchar(255),
city varchar(255),
postcode integer,
country varchar(255),
active integer,
constraint pk_customer primary key (id)
);

create table customer_login (
id integer auto_increment not null,
email varchar(255),
password varchar(255),
customer_number_reference integer,
constraint pk_customer_login primary key (id)
);


# --- !Downs

drop table if exists customer;

drop table if exists customer_login;


I would like to have the ID from customer showing as the cus number reference in customer_login
I would also like the email in the customer login table to be the same as that in customer.

What should my annotations look like to make this script generate these Fk's?

Cheers!

Koen De Groote

unread,
Apr 14, 2017, 2:49:50 PM4/14/17
to Play Framework
While this is more general Ebean related than actual Play related, what you're looking for is probably this: https://ebean-orm.github.io/docs/mapping/jpa/oneToMany

James Goodman

unread,
Apr 23, 2017, 7:16:45 PM4/23/17
to play-fr...@googlegroups.com
Cheers for this.

I ended up doing (on my customer table)

@OneToOne(mappedBy = "customer")
     public CustomerLogin customerLogin;

Above one of my fields

And this 

@OneToOne
    @JoinColumn(name = "id")
    public Customer customer;

on my customer login table..

It seems to have generated this on my sql script

create table customer (
  id                            integer auto_increment not null,
  first_name                    varchar(255),
  last_name                     varchar(255),
  phone_number                  varchar(255),
  email                         varchar(255),
  address1                      varchar(255),
  address2                      varchar(255),
  suburb                        varchar(255),
  city                          varchar(255),
  postcode                      integer,
  country                       varchar(255),
  active                        integer,
  constraint pk_customer primary key (id)
);

create table customer_login (
  id                            integer auto_increment not null,
  email                         varchar(255),
  password                      varchar(255),
  constraint uq_customer_login_id unique (id),
  constraint uq_customer_login_email unique (email),
  constraint pk_customer_login primary key (id)
);

Which I think is right .. I am not 100% but a few things I tried seemed to restrict things..

--
You received this message because you are subscribed to a topic in the Google Groups "Play Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/NPNY_mAU7Bs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/a6becfa9-193e-4633-ba1c-1b56c3e496c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages