Created
February 21, 2012 14:28
-
-
Save danielberndt/1876785 to your computer and use it in GitHub Desktop.
How to solve self-referential issues with ebeans/playframework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import models.QueueElement; | |
import play.*; | |
import play.mvc.*; | |
import views.html.*; | |
public class Application extends Controller { | |
public static Result create() { | |
QueueElement lastQe = null; | |
for (int i=0; i<10; i++) { | |
QueueElement qe = new QueueElement("queue element "+i); | |
qe.save(); | |
if (lastQe!=null) { | |
lastQe.successor = qe; | |
lastQe.save(); | |
} | |
lastQe = qe; | |
} | |
lastQe.save(); | |
return ok("created"); | |
} | |
public static Result index() { | |
return ok(index.render(QueueElement.find.all())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(list: List[models.QueueElement]) | |
<html> | |
<ul> | |
@for(qe <- list) { | |
<li>@qe.id - @qe.content</li> | |
} | |
</ul> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<ul> | |
<li>1 - queue element 0</li> | |
<li>2 - </li> | |
<li>3 - queue element 2</li> | |
<li>4 - </li> | |
<li>5 - queue element 4</li> | |
<li>6 - </li> | |
<li>7 - queue element 6</li> | |
<li>8 - </li> | |
<li>9 - queue element 8</li> | |
<li>10 - </li> | |
</ul> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.ManyToOne; | |
import play.db.ebean.Model; | |
@Entity | |
public class QueueElement extends Model { | |
@Id | |
public Long id; | |
@ManyToOne | |
public QueueElement successor; | |
public String content; | |
public static Finder<Long,QueueElement> find = new Finder<Long,QueueElement>(Long.class, QueueElement.class); | |
public QueueElement(String content) { | |
this.content = content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment