Skip to content

Instantly share code, notes, and snippets.

@jannylund
Created April 16, 2014 14:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jannylund/15a5a759b12d751cd419 to your computer and use it in GitHub Desktop.
Save jannylund/15a5a759b12d751cd419 to your computer and use it in GitHub Desktop.
Play Framework - Example on how to use @OneToMany with Ebean
# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions
# --- !Ups
create table relation (
id bigint not null,
user_id bigint,
constraint pk_relation primary key (id))
;
create table user (
id bigint not null,
constraint pk_user primary key (id))
;
create sequence relation_seq;
create sequence user_seq;
alter table relation add constraint fk_relation_user_1 foreign key (user_id) references user (id) on delete restrict on update restrict;
create index ix_relation_user_1 on relation (user_id);
# --- !Downs
SET REFERENTIAL_INTEGRITY FALSE;
drop table if exists relation;
drop table if exists user;
SET REFERENTIAL_INTEGRITY TRUE;
drop sequence if exists relation_seq;
drop sequence if exists user_seq;
package controllers;
import models.Relation;
import models.User;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index() {
User user = new User();
Relation relation1 = new Relation();
Relation relation2 = new Relation();
user.relationList.add(relation1);
user.relationList.add(relation2);
user.save();
Logger.debug("size of users: " + User.find.findRowCount());
Logger.debug("size of relations: " + Relation.find.findRowCount());
user.delete();
Logger.debug("size of users: " + User.find.findRowCount());
Logger.debug("size of relations: " + Relation.find.findRowCount());
return ok(index.render("Your new application is ready."));
}
}
[info] Set current project to demo (in build file:/private/tmp/demo/)
_
_ __ | | __ _ _ _
| '_ \| |/ _' | || |
| __/|_|\____|\__ /
|_| |__/
play 2.2.0 built with Scala 2.10.2 (running Java 1.7.0_51), http://www.playframework.com
> Type "help play" or "license" for more information.
> Type "exit" or use Ctrl+D to leave this console.
[demo] $ ~run
--- (Running the application from SBT, auto-reloading is enabled) ---
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)
[success] Compiled in 614ms
[info] play - database [default] connected at jdbc:h2:mem:play
[error] application -
! @6i0a80g2f - Internal server error, for (GET) [/] ->
play.api.db.evolutions.InvalidDatabaseRevision: Database 'default' needs evolution![An SQL script need to be run on your database.]
at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1$$anonfun$apply$1.apply$mcV$sp(Evolutions.scala:486) ~[play-jdbc_2.10.jar:2.2.0]
at play.api.db.evolutions.EvolutionsPlugin.withLock(Evolutions.scala:507) ~[play-jdbc_2.10.jar:2.2.0]
at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1.apply(Evolutions.scala:461) ~[play-jdbc_2.10.jar:2.2.0]
at play.api.db.evolutions.EvolutionsPlugin$$anonfun$onStart$1.apply(Evolutions.scala:459) ~[play-jdbc_2.10.jar:2.2.0]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:na]
at play.api.db.evolutions.EvolutionsPlugin.onStart(Evolutions.scala:459) ~[play-jdbc_2.10.jar:2.2.0]
[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Dev)
[debug] application - size of users: 1
[debug] application - size of relations: 2
[debug] application - size of users: 0
[debug] application - size of relations: 0
package models;
import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Created by jan on 2014-04-16.
*/
@Entity
public class Relation extends Model {
@Id
public Long id;
@ManyToOne
public User user;
public static Finder<Long,Relation> find = new Finder( Long.class, Relation.class );
}
package models;
import play.db.ebean.Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jan on 2014-04-16.
*/
@Entity
public class User extends Model {
@Id
public Long id;
@OneToMany(cascade = CascadeType.ALL)
public List<Relation> relationList = new ArrayList<>();
public static Finder<Long,User> find = new Finder( Long.class, User.class );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment