Fraska Portal

Exploring the WebSphere Commerce world

Play Framework: implement @OneToOne

Posted by on in Play Framework
  • Font size: Larger Smaller
  • Hits: 3970
  • 32 Comments
  • Subscribe to this entry
  • Print

Let's take a classic kind of relational DB relationship: One To One.

For example:

1 to 1 relationship

Userdemo contains supplementary information respect to the data User defines; it's actually an extension of User. In other words User can live without Userdemo but not the opposite.

I've implemented this same relationship in the following way:

User.java

package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import play.data.validation.Constraints.*;
import play.db.ebean.*;
import javax.persistence.*;

@Entity
public class User extends Model{
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long userId;
	
	@Required
	public String registertype;
	
	public Date registration;
	
	public Date lastsession;
	
	public Date registrationupdate;
	
	 @OneToOne(mappedBy="user",optional = true, cascade = CascadeType.ALL)
	 @PrimaryKeyJoinColumn
	 @JoinColumn(name="user_id")
	 public Userdemo userdemo;
	
	
	public User(Long userId, int languageId, String registertype, Userdemo userdemo, Userreg userreg) {
	      this.userId = userId;
	      this.languageId = languageId;
	      this.registertype = registertype;
	      this.userdemo = userdemo;
	      this.userreg = userreg;
	    }
	
	
	public static List all(){
		return find.all();
	}
	
	public static Finder<Long,User> find = new Finder(Long.class, User.class);
	
	public static void create(User user){
		user.save();
	}
	
	public static void delete(Long userId){
		find.ref(userId).delete();
	}
}


Userdemo.java

@Entity
public class Userdemo extends Model{
	@Id
	@GeneratedValue(generator = "fk")
    public Long userId; 
	
	@Required
	public String displayname;
	
	@Required
	public String email;
	
	public String name;
	
	public String surname;
	
	@OneToOne(optional=false)
	@PrimaryKeyJoinColumn
	public User user;
	
	public static List all(){
		return find.all();
	}
	
	public static Finder<Long,Userdemo> find = new Finder(Long.class, Userdemo.class);
	
	public static void create(Userdemo user){
		user.save();
	}
	
	public static void delete(Long userId){
		find.ref(userId).delete();
	}
}

Hopefully suggested implementation could save someone a bit of time

Rate this blog entry:
0

Comments

Leave your comment

Guest
Guest Monday, 15 July 2019

Most Popular Post

WebSphere Commerce, the curious life of a front-end catalog request
Core
Rate this blog entry:
5
WebSphere Commerce, the SOLR extension index
Administration
Rate this blog entry:
4
WebSphere Commerce, Data Load and SOLR Delta Index
Data Load
Rate this blog entry:
0

Latest Blogs

WebSphere Commerce, CommandLevelAuthorizationCache
Cache
Rate this blog entry:
0
WebSphere Commerce v8, toolkit exception, ClassNotFound db2
Administration
Rate this blog entry:
1
WebSphere Commerce, ATP migration
Store
Rate this blog entry:
0
WebSphere Commerce, the curious life of a front-end catalog request
Core
Rate this blog entry:
5
WebSphere Commerce, Performance analysis of few European stores
Performance
Rate this blog entry:
0