Mornor
Mornor

Reputation: 3783

Exclude fields from Ebean to save it in DB

I have a User bean :

@Entity
public class User extends Model{

    @Id
    public int id; 

    public String name; 
    public String username; 
    public String email; 
    public String password; 
    public Timestamp inscriptionDate; 
    public Timeline timeline;
}

Which I save in database with play like this:

User user = Form.form(User.class).bindFromRequest().get();
user.save();

But I do not want to save the Timeline User's fields.

Is there a way to achieve this goal?

EDIT 1
I tried @Transient on the wanted field, bt no effect.

EDIT 2
Here is the Timeline class :

import java.util.Collection;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Timeline {

    public SortedSet<Event> events;
    public String familyName;    

    public Timeline(String familyName)
    {       
        this.events=new TreeSet<Event>(Event.getEventsComparator());
        this.familyName=familyName;
    }
// some other methods 
}

And here is the User class :

@Entity
public class User extends Model{

@Id
public int id;  
public String name; 
public String username; 
public String email; 
public String password; 
public Timestamp inscriptionDate;
@Transient
public Timeline timeline;

public User(){}

public User(String name, String email, String username, String password){
    this.name = name; 
    this.email = email; 
    this.username = username; 
    this.password = password; 
}

// Other methods ... 
}

EDIT 3
I displayed my user in db using this method :

public static Result getUsers(){
        List<User> users = new Model.Finder(String.class, User.class).all(); 
        return ok(toJson(users)); 
    }

An I think this i why I always have a null field for timeline. Timeline is actually not saved in DB, am I right ?

Upvotes: 1

Views: 2032

Answers (2)

Karim BENHDECH
Karim BENHDECH

Reputation: 381

I use @Transient in my Application with success.

Perhaps is an import problem :

import javax.persistence.Transient

not

import java.beans.Transient

Edit 2

Exclude field in DB : @Transient works

Exclude field in json : It exist few solutions, view is one

Upvotes: 2

Mon Calamari
Mon Calamari

Reputation: 4463

Binding data directly from request (form) to database entity class is a security breach. You would want to bind it to POJO class, perform validation if necessary and then populate your User.class with request data yourself.

Upvotes: 0

Related Questions