0

Hello I am trying to make a table in MySQL using Spring Boot and JPA and I am trying to make one of the columns in the table be a Java Class aka a JSON object is there any way I could do this and is there any examples or documentation for this solution.

Example of Table that I made

import com.project.something.here.Userdata

@Entity
@Table(name = "User")
public class Exercises {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    @Column(name = "user_name")
    private String username;
    @Column(name = "user_description")
    private String userdescription;
    @Column(name = "user_link")
    private String userlink;

    //here is where I am trying to set one of the columns as a Java class or a JSON object.
    private Userdata userdata;
jaffer_syed
  • 141
  • 1
  • 3
  • 7
  • Just use `@ManyToOne`? – dan1st Apr 03 '20 at 15:41
  • If Userdata is saved in a table, use ManyToOne. If its just an object, add @Transient https://stackoverflow.com/questions/2154622/why-does-jpa-have-a-transient-annotation – Sully Apr 03 '20 at 20:42

1 Answers1

0

I believe you are more looking with one-to-one mapping in JPA. Please have a look : https://www.baeldung.com/jpa-one-to-one

Please have a look if that helps you.

import com.project.something.here.Userdata;

@Entity
@Table(name = "User")
public class Exercises {
    // ...

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(unique = true)
    private Userdata userdata;

    // ...
}

Userdata.java

@Entity
@Table(name = "userdata")
public class Userdata {

    @Id
    @Column(name = "id")
    private Long id;

    //...

    @OneToOne(mappedBy = "userdata")
    private Exercises exercises;

    //... getters and setters
}
Sully
  • 14,672
  • 5
  • 54
  • 79
vijay
  • 16
  • 3