1

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       
    @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

Do I need to set LenameFk manually in Le entity since I am getting *Cannot insert the value NULL into column 'LENAME_FK'*? Why won't it automatically take it from Le enityt?

4

2 回答 2

0

注意这段代码:

public class Le implements Serializable {

...

@ManyToOne(optional = false, fetch = FetchType.LAZY) 

private Letype LenameFk;

...

}

optional = false表示该实体的任何实例都必须参与关系,因此外键字段不能为 null

于 2013-11-24T12:22:59.367 回答
0

您的权利,您需要在实体中LenameFk手动设置。Le

一般来说,对于双向一对多双向关系,Accessor方法应该如下所示,并假设实体是CustomerOrder一对多关系。

客户.java

public Collection<Order> getOrders() {
    return Collections.unmodifiableCollection(orders);
}

public void addToOrders(Order value) {
    if (!orders.contains(value)) {
        orders.add(value);
        value.setCustomer(this);
    }
}

public void removeFromOrders(Order value) {
    if (orders.remove(value)) {
        value.setCustomer(null);
    }
}

订单.java

public void setCustomer(Customer value) {
    if (this.customer != value) {
        if (this.customer != null) {
            this.customer.removeFromOrders(this);
        }
        this.customer = value;
        if (value != null) {
            value.addToOrders(this);
        }
    }
}

public Customer getCustomer() {
    return customer;
}
于 2013-11-28T05:32:35.513 回答