0

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).

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
  • possible duplicate of [Is it possible to dynamically define column names in Hibernate / JPA?](http://stackoverflow.com/questions/3617687/is-it-possible-to-dynamically-define-column-names-in-hibernate-jpa) – Luiggi Mendoza May 09 '14 at 07:05

1 Answers1

0

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>();
Wundwin Born
  • 3,467
  • 19
  • 37