我需要在JPA中设置外键值吗?

时间:2013-11-11 05:36:18

标签: jpa-2.0

我有两张桌子:

 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

LETYPE中的一个元组会有很多LE。

由netbeans生成的JPA实体:

     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;
}

现在,我想要的是如果我从JSF视图添加LETYPE,我想同时添加多个LE。

LETYPE 
      -LE1
      -LE2
      -LE3

我是否需要在 Le 实体中手动设置 LenameFk *无法将值NULL插入列'LENAME_FK'* ?为什么不自动从 Le enityt中获取它?

2 个答案:

答案 0 :(得分:0)

请注意以下代码段:

public class Le implements Serializable {

...

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

private Letype LenameFk;

...

}

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

答案 1 :(得分:0)

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

一般情况下,对于双向一对多双向关系,Accessor方法应如下所示,并假设实体为CustomerOrder以及一对多关系b / w他们。

<强> Customer.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);
    }
}

<强> Order.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;
}