java - Spring Data JPA user posts -
i have user login , profile view, users have posts. can guide me in right direction?
i have user entity:
@entity @table(name = "usr", indexes = { @index(columnlist = "email", unique = true) }) // using usr because in may conflict name of class public class user { public static final int email_max = 250; public static final int name_max = 50; /* * public static enum role { * * unverified, blocked, administrator * * } */ // primary key long, needs annotated @id @id @generatedvalue(strategy = generationtype.identity) private long id; // add columns @column(nullable = false, length = email_max) private string email; @column(nullable = false, length = name_max) private string name; // no length, password encrypted longer value // user enters @column(nullable = false) private string password; /* * //email verification code * * @column(length = 16) private string verificationcode; * * public string getverificationcode() { return verificationcode; } * * public void setverificationcode(string verificationcode) { * this.verificationcode = verificationcode; } * * * @elementcollection(fetch = fetchtype.eager) private set<role> roles = new * hashset<role>(); * * * * public set<role> getroles() { return roles; } * * public void setroles(set<role> roles) { this.roles = roles; } */ public long getid() { return id; } public void setid(long id) { this.id = id; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public boolean iseditable() { user loggedin = mytools.getsessionuser(); if (loggedin == null) { return false; } return loggedin.getid() == id; } }
and repo:
public interface userrepository extends jparepository<user, long> { // @query("select u user u u.email = ?1") user findbyemail(string email); }
now, in order have posts user, create posts entity , repository @manytoone in post pojo? i'm trying make twitter first gotta users post. if know of tutorial explaining that'd great.
create second entity (java class) e.g. userpost:
@entity @table(...) public class userpost { @id @generatedvalue(strategy = generationtype.identity) private long id; private long userid; ... }
then add @onetomany relationship field user. cascading, lazy-loading, etc. depends on how you'd use it. it'd inside user:
@onetomany(cascade={...}) @joincolumn(name="userid") private set<userpost> posts;
Comments
Post a Comment