且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在同一个表中使用列表或在JPA 2.1中设置多列

更新时间:2022-12-11 23:26:30

至少,需要将实体属性映射到表列。
因此,只需要为实体中的每个OPTIONS设置属性(变量,getter和setter);

  ... 
@Column(name =OPTION)//这里是OPTION1,OPTION2,等等
private String option;
...

另一种方法是为OPTION创建一个单独的表和一个实体满足您的要求。
问题实体可以与选项实体(许多单向或双向关系 - 下面是使用的示例双向

  @Entity 
@Table(name =OPTION)
public class Option {
@Id
@GeneratedValue
@Column(name = )//表中的列名
私有长ID;

@Column(name =)
private String optionTitle; //可以为OPTION1,OPTION2等使用枚举

@Column(name =)
private String optionValue;

@ManyToOne
@JoinColumn(name =QUESTION_ID,nullable = false)// QUESTION_ID(无论您在Option表中定义了外键列)
private问题问题;

..
//构造函数,getters& setters





$ b在问题实体中,为选项集合(List,Set和any)添加属性)

  @OneToMany(mappedBy =question)
private List< Option> optionList = new ArrayList< Option>();


I am new with JPA. I am building sample maven project with Wicket-Spring-Hibernate-JPA.

I am using Hibernate EntityManager: v4.3.5.Final. I am using JPA: v2.1-api. I am using database mySql: v5.6.

When I am writing a model class, I want to map tables multiple columns with an element in model class as List or Set.

Database Table Name:  QUESTION_MAIN
Column Name : Type : PK
QID         : int : PK
QTYPE       : varchar
QUESTION    : varchar
OPTION1     : varchar
OPTION2     : varchar
OPTION3     : varchar
OPTION4     : varchar
OPTION5     : varchar
OPTION6     : varchar

Note: Assuming that there will be maximum 6 options to question.

Now in model class, I want to represent this like:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="QUESTION_MAIN")
public class Question {

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

   @Column(name="QTYPE")
   private String quest_type;

   @Column(name="QUESTION")
   private String question;

   //TODO: Create List or Set for OPTIONS
}

For the line in code:

//TODO: Create List or Set for OPTIONS

I want to create a List or Set for the options columns here.

Please do let me know any possible way for this. Or if anyone aware of such situation, provide any alternative to this.


Hello Luiggi, I am not sure the above link you provided solves my issue. Still I am trying to understand the option provided in that link. What I am looking for is, can I be able to create a List or Set of ColumnNames as one element in our Bean Class (with JPA Annotations).

At least, need to map entity property to table column. So simply need property (variable, getter&setter) for each OPTIONS in entity;

...
@Column(name="OPTION") //here OPTION1, OPTION2, and so on
private String option;
...

Another way is to create a separate table and an entity for OPTION to fulfill your requirement. From Question entity can do relationship to Option entity (one-to-many uni or bidirectional relationship - below example used bidirectional)

@Entity
@Table(name="OPTION")
public class Option {
    @Id
    @GeneratedValue
    @Column(name="") //column name in table
    private Long id;

    @Column(name="")
    private String optionTitle; //can use enum for OPTION1, OPTION2, and so on

    @Column(name="")
    private String optionValue;

    @ManyToOne
    @JoinColumn(name = "QUESTION_ID", nullable = false)   //QUESTION_ID (whatever you defined foreign key column in Option table)
    private Question question;

    ..
    //constructor, getters & setters
}

In Question entity, add property for option collection (List, Set and any sortable collection as you needed)

@OneToMany(mappedBy = "question")
private List<Option> optionList = new ArrayList<Option>();