kinkajou
kinkajou

Reputation: 3728

JPA entity and Database Table mapping Validation constraint(s) violated

I have two table:

 CREATE TABLE [LeTYPE](
    [LeNAME] [varchar](100) NOT NULL,
    [Le_DESC] [varchar](500) NULL,
    [LeFOR] [varchar](50) NOT NULL,
 CONSTRAINT [PK_LeTYPE] PRIMARY KEY CLUSTERED 
(
    [LeNAME] ASC
)
)

CREATE TABLE [Le](
    [SN] [int] IDENTITY(1,1) NOT NULL,
    [LeNAME_FK] [varchar](100) NOT NULL,
    [Le_SN] [int] NULL,
    [LOWERRANGE] [float] NOT NULL,
    [UPPERRANGE] [float] NOT NULL,
    [Le_DESC] [varchar](500) NULL,
    [COLOR] [varchar](45) NULL,
 CONSTRAINT [Le_pk] PRIMARY KEY CLUSTERED 
(
    [SN] ASC
))
GO


ALTER TABLE [Le]  WITH CHECK ADD  CONSTRAINT [FK_Le_LeTYPE] FOREIGN KEY([LeNAME_FK])
REFERENCES [LeTYPE] ([LeNAME])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [Le] CHECK CONSTRAINT [FK_Le_LeTYPE]
GO

One tuple in LETYPE will have many LE.

JPA Entity generated by netbeans:

     public class Letype implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(nullable = false, length = 100)
    private String Lename;
    @Size(max = 500)
    @Column(name = "Le_DESC", length = 500)
    private String LeDesc;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(nullable = false, length = 50)
    private String Lefor;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "LenameFk", fetch = FetchType.LAZY)
    private List<Le> LeList;
}



public class Le implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private Integer sn;
    @Column(name = "Le_SN")
    private Integer LeSn;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private double lowerrange;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private double upperrange;
    @Size(max = 500)
    @Column(name = "Le_DESC", length = 500)
    private String LeDesc;
    @Size(max = 45)
    @Column(length = 45)
    private String color;
    @JoinColumn(name = "LeNAME_FK", referencedColumnName = "LeNAME", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Letype LenameFk;
}

Now, What I wanted was if I add a LETYPE from JSF view I would like to add multiple LE also at the same time.

LETYPE 
      -LE1
      -LE2
      -LE3

The structure of entities generated by netbean has letypeList with in Le and not the opposite. Is my database structure wrong or How to do it right?

Upvotes: 0

Views: 1125

Answers (2)

Omar
Omar

Reputation: 1440

"What I wanted was if I add a LETYPE from JSF view I would like to add multiple LE also at the same time" means that the LETYPE can be seen as the component class, and the LE as the composite class, so you should reverse the mapping annotations. To illustrate this and what @Jayasagar well explained in addition, the two classes's forms look like :

Letype.java :

public class Letype implements Serializable {
    ...
    @OneToMany(mappedBy="letype")
    private List<Le> les;
}

Le.java :

public class Le implements Serializable {
    ...
    @ManyToOne
    @JoinColumn(name = "Letype_FK", referencedColumnName = "LENAME")
    private Letype letype;
}

Upvotes: 1

Jayasagar
Jayasagar

Reputation: 2066

Yes i feel like you got wrong relationship in database between LE and LETYPE.

Current relation from JPA perspective is treated as One LE can have Many LETYPE that is why you see below code in LE entity class.

     @OneToMany(mappedBy = "LeFk")
     private List<Letype> letypeList; 

But it is wrong, you need Reverse relation i.e, One LETYPE can have Many LE (One-To-Many).

Basically your database structure is wrong I guess, you have to maintain FK column in LE table, where as currently you maintaining in LETYPE.

In database world, FK column always resides in Many side , in your case i.e. LE table.

What to do now

  • Remove FK column in LETYPE table
  • Add FK column in LE table referring to LETYPE table primary key
  • Generate JPA Entity again
  • Then you see right JPA code with right relation ship.

I hope then you should be able to do what you need.

Upvotes: 2

Related Questions