如何使 MySQL 中的列容纳 Java 类。

huangapple go评论81阅读模式
英文:

How to make a column in mysql hold a Java class

问题

你好,我正在尝试使用Spring Boot和JPA在MySQL中创建一张表,我想让表中的一列是一个Java类,也就是一个JSON对象,我是否有办法实现这个,并且是否有相关示例或文档可以参考。

我已创建的表格示例:

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;

    // 这里是我尝试将其中一列设置为Java类或JSON对象的地方。
    private Userdata userdata;
}
英文:

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;

答案1

得分: 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

请查看这个链接,看看是否对您有帮助。

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
}
英文:

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
}

huangapple
  • 本文由 发表于 2020年4月3日 23:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61015400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定