我正在尝试根据JSF表单的输入保存一个名为Benchmark
的新实体。
我不确定如何继续。在Benchmark
中,有一个名为article
的关系属性。 article
已经在数据库中,但我需要为我要插入的新Benchmark
实体添加对它的引用。 Article
有一个名为AID
的唯一ID,但这不是用户所知道的。用户使用Article
字段Art
和Supplier
来标识唯一的Article
实体/数据库表行。但我不确定如何使用该输入信息来设置article
实体中的Benchmark
字段。通常我会为testfield设置一个转换器,它将字符串值转换为正确的实体类型(就像我为Antigen / target做的那样),但由于有两个输入字段,我无法看到我会怎么做。
有关如何进行的任何建议/提示?
这是(大致)我的Benchmark
实体:
@Entity
@NamedQueries({
@NamedQuery(name = "Benchmark.findAll", query = "SELECT b FROM Benchmark b"),
@NamedQuery(name = "Benchmark.countTotal", query = "SELECT COUNT(b) FROM Benchmark b")})
public class Benchmark implements Serializable {
private static final long serialVersionUID = 1L;
public static final String ALL = "Benchmark.findAll";
public static final String COUNT = "Benchmark.countTotal";
@Id
private Integer ID;
@Column(name="ba_type")
private String type;
@JoinColumn(name = "target", referencedColumnName = "PID", insertable = false, updatable = false)
@ManyToOne(optional = false)
private MuxProtein target;
@JoinColumn(name = "benchmark_assay", referencedColumnName = "AID", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Article article;
@JoinColumn(name = "sample_type", referencedColumnName = "ID", insertable = false, updatable = false)
@ManyToOne(optional = false)
private SampleType sampleType;
@JoinColumn(name = "target", insertable=false, updatable=false,referencedColumnName = "PID")
@ManyToOne(optional = true)
private MuxProtein proteinPID;
...
}
这就是形式:
<h:form id="addDilForm">
<p:panelGrid >
<f:facet name="header" >
<p:row><p:column colspan="2">New benchmark</p:column></p:row>
</f:facet>
<p:row>
<p:column colspan="1">
<h:outputLabel>Sample</h:outputLabel>
</p:column>
<p:column>
<p:selectOneMenu value="#{benchController.benchy.sampleType}" converter="SampleTypeConverter">
<f:selectItems value="#{SampleTypeController.samples}" var="samp" itemLabel="#{samp.name}" itemValue="#{samp}" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column colspan="1">
<h:outputLabel>Antigen</h:outputLabel>
</p:column>
<p:column >
<p:inputText value="#{benchController.benchy.target}" converter="MuxProteinConverter" />
</p:column>
</p:row>
<p:row>
<p:column rowspan="2">
<h:outputLabel>Benchmark assay</h:outputLabel>
</p:column>
<p:column>
<h:outputLabel value="Articlenumber " /><p:inputText />
</p:column>
</p:row>
<p:row>
<p:column><h:outputLabel value="Supplier " /><p:selectOneMenu ><f:selectItems value="#{supplier.suppliers}" var="supp" itemLabel="#{supp.supplier}" /></p:selectOneMenu></p:column>
</p:row>
<f:facet name="footer">
<p:row>
<p:column colspan="2">
<h:commandButton value="Save" action="#{dilution.save()}" />
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
</h:form>
我的Supplier
实体:
@Entity
public class Supplier implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "SID")
private Short sid;
@Basic(optional = false)
@Column(name = "Supplier")
private String supplier;
@OneToMany(cascade = {CascadeType.MERGE,CascadeType.REMOVE,CascadeType.REFRESH}, mappedBy = "supplierSID")
private List<Article> articleList;
...
}
我的Article
实体:
@Entity(name="MuxArticle")
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "AID")
private Integer aid;
@Basic(optional = false)
@Column(name = "Art")
private String art;
@Column(name = "Price")
private String price;
@JoinColumn(name = "supplier_SID", referencedColumnName = "SID")
@ManyToOne(optional = false)
private Supplier supplierSID;
...
}