3

I have a MySQL table where the dataType for the attribute workingDays is SET. something like this:

CREATE TABLE IF NOT EXISTS `course` (
`courseId` int(11) NOT NULL AUTO_INCREMENT,
`courseName` varchar(255) NOT NULL DEFAULT '0',
`courseDescription` varchar(255) DEFAULT NULL,
`courseDuration` int(11) DEFAULT NULL,
`courseBatchSize` int(11) NOT NULL DEFAULT '0',
`courseEnrolmentDate` date DEFAULT NULL,
`workingDays` set('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') DEFAULT NULL,
`courseFee` float DEFAULT NULL,
`isScholarshipAvailable` enum('Y','N') DEFAULT NULL,
`scholarshipCriteria` varchar(255) DEFAULT NULL,
`scholarshipExamDate` date DEFAULT NULL,
`successRate` tinyint(4) DEFAULT NULL,
`rating` tinyint(4) DEFAULT NULL,
`otherInfo` varchar(255) DEFAULT NULL,
`courseDiscount` float DEFAULT NULL,
`institute` int(11) DEFAULT NULL,
`exam` int(11) DEFAULT NULL,
PRIMARY KEY (`courseId`),
KEY `FK_INS` (`institute`),
KEY `FK_EX` (`exam`),
CONSTRAINT `FK_EX` FOREIGN KEY (`exam`) REFERENCES `exam` (`examId`),
CONSTRAINT `FK_INS` FOREIGN KEY (`institute`) REFERENCES `institute` (`instituteId`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 COMMENT='contains course info';

Now correspondingly I want to map with the pojo using jpa annotations. Presently I am doing it like this

@Column(name = "workingDays", nullable = false, unique = false)
@Type(type="org.hibernate.type.settype")
private Set<String> workingDays;

where the annotation @Column is from package javax.persistence. With @Type annotation which is from hibernate package I am getting an error

    Caused by: java.lang.ClassNotFoundException: org.hibernate.type.settype

My pom.xml file has the following dependencies added

    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>


    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.5.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.4.Final</version>
    </dependency>

    <!-- Hibernate library dependecy start -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>
    <!-- Hibernate library dependecy end -->

I am not sure how to resolve this issue. any insight or other workaround for mapping to a set in MySQL database will be helpful.

EDIT: Also suggest if I can store the working days attribute in database without using SET. This working day attribute is for a particular Company class and I need to show what are the working days of that company to end user. Suggestion regarding the datatype will be really helpful too.

Thanks.

2
  • It is org.hibernate.type.SetType not org.hibernate.type.settype Commented May 30, 2014 at 7:18
  • Thanks. However, Now I am getting this error Caused by: org.hibernate.MappingException: Could not instantiate Type: org.hibernate.type.SetType Could you provide an example how to use this annotation? I am fairly new to hibernate Commented May 30, 2014 at 7:21

1 Answer 1

0

You don't need the @Type(type="org.hibernate.type.setType") declaration on simple types. You may remove it from the field declaration.

@Type annotation is required only when you are working with advanced custom types

class Account {

  @Type(type="com.something.type.CustomDateType")
  @Column(name = "OPEN_DATE")
  private CustomDate date;

}
Sign up to request clarification or add additional context in comments.

3 Comments

So if I am using datatype SET in mysql database which annotation to be used and what should be the data type in java side?
database table structure is not clear in your question. Please update it with the complete structure.
Unhelpful. This does not answer the question. The question is how to map a MySQL set column (dev.mysql.com/doc/refman/8.4/en/set.html) in an entity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.