Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use a entity with @RedisHash in Redis and in a relational Database?

I am using Spring boot. To save my entities on relational database, I configured a datasource and my domain classes, e.g.:

@Entity
@Table(schema = "schema_name", name = "tb_name")
public class table_name extends DomainEntity<Long> {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID_TABLE_NAME", nullable = false, updatable = false, precision = 12)
    @SequenceGenerator(name = "sqTableName", sequenceName = "SQ_TABLE_NAME", initialValue = 1, allocationSize = 1)
    @GeneratedValue(generator = "sqTableName", strategy = GenerationType.SEQUENCE)
    private Long id;

    @NotNull  
    @ManyToOne
    @JoinColumn(name   = "ID_OTHER_COLUMN", referencedColumnName = "ID_OTHER_COLUMN", nullable = false)
    private OtherObject obj;

Using this tutorial: https://www.baeldung.com/spring-data-redis-tutorial, I configured my domain class Student:

@RedisHash("Student")
public class Student implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public enum Gender {
        MALE, FEMALE
    }

    private String id;
    private String name;
    private Gender gender;
    private int grade;
}

When a class is anotated with @RedisHash, when I use .save method, it saves this entity on Redis.

I would like to use this domain class in a relational database WITHOUT duplicating the file to save, sometimes on Redis and sometimes in a relational database. I searched, but I didn't find anything.

Can someone help?

like image 789
Lucas Amorim Silva Avatar asked Nov 18 '22 00:11

Lucas Amorim Silva


1 Answers

You could define the domain class without the annotation @RedisHash and make an empty subclass of the domain class for each database.

The domain class for redis would have the @RedisHash annotation

like image 115
keller Avatar answered Dec 15 '22 04:12

keller