algorithmic
algorithmic

Reputation: 137

Optimized way for mapping Hibernate annotations One to Many Relationship

I am working on a Spring MVC WEB App and I have difficulty to apply a "good" mapping design with Hibernate in my Entity classes with the proper annotation. I would like to have some advice on what will be the best design to represent my database tables in Java.

For understanding my situation: The goal of this ORM mapping will be used to persist existing unmarshalled xml files in the SentMessage table. In the FeedBackMessage table I will persist already existing CSV files (feedbacks) parsed to Java objects.

Based on this 'batch process' I must do comparisons to know which documentid received a possible number of status(statuses) based on the feedbacks received on database level.

I have tried with following design but i got "detached entity errors" when i try to insert data in database:

SentMessage Entity

@Entity
@Table(name = "SENTMESSAGES")
public class SentMessaages{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "documentid")
    private String documentid;

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

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

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

    /***************************************/
    Setters en Getters method
    //****//
}

Feedbackmessage Entity

@Entity
@Table(name = "FEEDBACKMESSAGES")
public class FeedbackMessage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer Id;

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

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

    @Column(name = "returndate")
    private Date returndate;

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

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


        Other fields...
/***************************************/
    Setters en Getters method
    //****//
}

CREATE STATEMENTS FOR TABLE IN MYSQL

CREATE TABLE `sentmessages` (
  `documentid` varchar(45) DEFAULT '',
  `language` varchar(30) DEFAULT NULL,
  `sending_date` date DEFAULT NULL,
  `customtext07` varchar(45) DEFAULT NULL,
  `customtext12` varchar(45) DEFAULT NULL,
  `customerid` varchar(45) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `documenttitle` varchar(45) DEFAULT NULL,
  `count` int(11) DEFAULT NULL,
  PRIMARY KEY (`documentid`)
) ENGINE=InnoDB;



CREATE TABLE `feedbackmessages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `documentid` varchar(45) DEFAULT NULL,
  `status` varchar(45) DEFAULT NULL,
  `endcustomerid` varchar(45) DEFAULT NULL,
  `reason` varchar(45) DEFAULT NULL,
  `actiondate` date DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `batchid` varchar(150) DEFAULT NULL,
  `filename` varchar(45) DEFAULT NULL,
  `channel` varchar(45) DEFAULT NULL,
  `islegal` varchar(45) DEFAULT NULL,
  `action` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX `FK_ID` (`document_id`),
  CONSTRAINT `FK_DOCID` FOREIGN KEY (`document_id`) REFERENCES `sentmessages` (`documentid`)

) ENGINE=InnoDB;

Upvotes: 0

Views: 113

Answers (1)

nkmuturi
nkmuturi

Reputation: 456

I believe you want a parent (sentmessage) / child (feedbackmessages) relationship mapping? If so, the mapping is like this (inside child class):

@ManyToOne
  @JoinColumn(name="documentid",
    referencedColumnName = "documentid"
  private SentMessage sentMessage;

This article might help you with Hibernate entity mappings in general: http://www.objectdb.com/java/jpa/entity

Upvotes: 1

Related Questions