Saving an object in Entity without saving it in JPA

I am using an application in the play platform in which I need to save the same instance of a non-Entity object to a JPA object without storing it in the database, I want to know if this can be achieved or not use annotations. An example of the code I'm looking for is:

 public class anEntity extends Model {
    @ManyToOne
    public User user;

    @ManyToOne
    public Question question;


    //Encrypted candidate name for the answer
    @Column(columnDefinition = "text")
    public BigInteger candidateName;

    //I want that field not to be inserted into the database
    TestObject p= new TestObject();

I tried the @Embedded annotation, but suggested that it embeds the fields of an object in an entity table. Do I need to use @Embedded when saving an object column in an entity table?

+3
source share
1 answer

Look @Transient abstract:

" , . , ."

, Singleton, getInstance() :

:

public class anEntity extends Model {
    @Transient
    private TransientSingleton t;

    public anEntity(){ // JPA calls this so you can use the constructor to set the transient instance.
        super();
        t=TransientSingleton.getInstance();
    }


public class TransientSingleton { // simple unsecure singleton from wikipedia

    private static final TransientSingleton INSTANCE = new TransientSingleton();
    private TransientSingleton() {
        [...do stuff..]
    }
    public static TransientSingleton getInstance() {
        return INSTANCE;
    }
}
+7

All Articles