0

I have an enum class that contain multiple data ,I want to store that enum data as list in database. for example Developer table it contain technology column ,that column store enum data, Suppose one user want to store Developer1 working on many technologies like java , Kotllin , .Net etc these technologies are belongs from enum class , How to store . Same as I have a Subject enum class that contain multiple subject name ,When I Register a new teacher then I want to store how many subjects teacher know ,If he know multiple subjects that present in our enum class list then store the subjects id which known by the teacher.But I am not able to store multiple data in a single column in subjectId ,It store only one Data in SubjectId column,If I pass multiple data in subjectId column in postman it throws error 400 teacherEntity class

package com.nilmani.jpqlprojectexample.entity

import com.nilmani.jpqlprojectexample.enum.Subject
import com.nilmani.jpqlprojectexample.enum.University
import java.util.*
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class Teacher(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id:Long=-1,
    val nameOfTeacher:String="",
    val price:Long=-1,
    val subject:Int = Subject.MATH.type,//List<Int> = listOf(Subject.MATH.type)
    val university:Int=University.OTHER.type
)

Subject enum class

package com.nilmani.jpqlprojectexample.enum

enum class Subject(val type:Int) {
    MATH(1),
    PHYSIC(2),
    ACCOUNTING(3),
    ZOOLOGY(4),
    BIOLOGY(5),
    PROGRAMMING(6),
    STATICS(7),
    CHEMISTRY(8),
    HISTORY(9)
}

ReqTeacher Model class

package com.nilmani.jpqlprojectexample.model.request

import com.nilmani.jpqlprojectexample.enum.Subject import com.nilmani.jpqlprojectexample.enum.University

data class ReqTeacher(
    val nameOfTeacher:String="",
    val price:Long=-1,
    val subject:Int= Subject.MATH.type,
    val university:Int= University.OTHER.type, )

Response Teacher Model class

package com.nilmani.jpqlprojectexample.model.response

import com.nilmani.jpqlprojectexample.enum.Subject
import com.nilmani.jpqlprojectexample.enum.University

data class ResTeacher(
    val nameOfTeacher:String="",
    val price:Long=-1,
    val subject:Int= Subject.MATH.type,
    val university:Int= University.OTHER.type,
)

Teacher controller class

package com.nilmani.jpqlprojectexample.controller

import com.nilmani.jpqlprojectexample.entity.Teacher
import com.nilmani.jpqlprojectexample.model.request.ReqTeacher
import com.nilmani.jpqlprojectexample.model.response.ResTeacher
import com.nilmani.jpqlprojectexample.repository.TeacherRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/teachTest")
class TeacherController {
    @Autowired
    private lateinit var teachRopo:TeacherRepository

    @PostMapping("/add")
    fun addTeacher(@ModelAttribute request:ReqTeacher):ResponseEntity<*>{
//        val newTeacher = teachRopo.findById(request.)
        var newTeacher = Teacher(
            nameOfTeacher = request.nameOfTeacher,
            price = request.price,
            subject = request.subject,
            university = request.university,
        )
        val saveTeacher = teachRopo.save(newTeacher)
        val respTeacher = ResTeacher(
            saveTeacher.nameOfTeacher,
            saveTeacher.price,
            saveTeacher.subject,
            saveTeacher.university
        )
        return ResponseEntity(respTeacher,HttpStatus.OK)
    }
}

I want to store multiple data or single data in SubjectId column of a particular teacherId,But My code store only one data for a particular teacherId

JUMBOTURN
  • 69
  • 1
  • 2
  • 11

1 Answers1

0

You should have a new table (Entity) that represents the 1-to-many relationship between Teacher and Subject, e.g. teacher_subject. This is the proper approach. If you attempt to store the subjects as space/comma-separated values in a single cell, e.g. "math, physics, biology" that would be bad practice.

Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41
  • Subject is not a entity class it just an enum class how it possible to establish one to many relation between Teacher entity class and Subject enum class – JUMBOTURN Jan 04 '22 at 08:25
  • 1
    I found this answer on SO that uses `@ElementCollection` and `@CollectionTable`: https://stackoverflow.com/a/3153172/9698467 – Stefan Zhelyazkov Jan 04 '22 at 09:24