我在数据库中有两个表:预订和类别。现在我想制作用户可以在Book表中添加书籍的页面,但是从Category表中选择合适的类别。
我可以将图书添加到表格中,但我无法在图书表格中保存值类别。
正如您所看到图书表中的类别 外键, category_id 来自类别< / strong> table。
以下是模型类:
预订模型
AA<-read.csv("~/example.csv",header = T,sep = ";",dec = ",")
AA$st <-
shingle(Indl.Data$t,
intervals = rbind(c(0, 14),
c(23, 26)))
AA<-AA[with(AA, order(t)), ]
my.panel.1 <- function(x, y, subscripts, col, pch,cex,sd,...) {
low95 <- y-sd[subscripts]
up95 <- y+sd[subscripts]
panel.xyplot(x, y, col=col, pch=pch,cex=cex, ...)
panel.arrows(x, low95, x, up95, angle=90, code=3,lwd=3,
length=0.05, alpha=0.5,col=col)
}
xyplot( logOD~t|cs+st,
data=AA,
strip = T,
sd=0,
groups=cs,
xlab = list("Time ", cex=1.5),
ylab = list("growth", cex=1.5),
type="p",
col=c("red","black"),
scales = list(x = "free"), between = list(x = 0.5),
panel.groups="my.panel.1",
panel="panel.superpose",
par.settings = list(layout.widths = list(panel = c(6, 2))))
}
类别模型
@Entity
@Table(name = "book")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b"),
@NamedQuery(name = "Book.findByBookId", query = "SELECT b FROM Book b WHERE b.bookId = :bookId")})
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "book_id")
private Integer bookId;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "name")
private String name;
@Lob
@Size(max = 65535)
@Column(name = "description")
private String description;
@JoinColumn(name = "category", referencedColumnName = "category_id")
@ManyToOne
private Category category;
public Book() {
}
public Book(Integer bookId) {
this.bookId = bookId;
}
public Book(Integer bookId, String name) {
this.bookId = bookId;
this.name = name;
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public int hashCode() {
int hash = 0;
hash += (bookId != null ? bookId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Book)) {
return false;
}
Book other = (Book) object;
if ((this.bookId == null && other.bookId != null) || (this.bookId != null && !this.bookId.equals(other.bookId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.biblioteka.app.domen.Book[ bookId=" + bookId + " ]";
}
}
现在我有JSF页面,我将bookes添加到数据库。我有下拉列表,将类别加载到其中。用户应选择一个类别并将书保存到表格。
这是来自 JSF addBook page 的代码。
@Entity
@Table(name = "category")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
@NamedQuery(name = "Category.findByCategoryId", query = "SELECT c FROM Category c WHERE c.categoryId = :categoryId")})
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "category_id")
private Integer categoryId;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "name")
private String name;
@Lob
@Size(max = 65535)
@Column(name = "description")
private String description;
@OneToMany(mappedBy = "category")
private Collection<Book> bookCollection;
public Category() {
}
public Category(Integer categoryId) {
this.categoryId = categoryId;
}
public Category(Integer categoryId, String name) {
this.categoryId = categoryId;
this.name = name;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlTransient
public Collection<Book> getBookCollection() {
return bookCollection;
}
public void setBookCollection(Collection<Book> bookCollection) {
this.bookCollection = bookCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (categoryId != null ? categoryId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Category)) {
return false;
}
Category other = (Category) object;
if ((this.categoryId == null && other.categoryId != null) || (this.categoryId != null && !this.categoryId.equals(other.categoryId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.biblioteka.app.domen.Category[ categoryId=" + categoryId + " ]";
}
正如您所看到的,我使用 selectOneMenu 并使用值 bookBean.category 然后我不确定我需要在 selectItems
这是 BookBean 代码:
<p:layoutUnit position="center">
<h:form>
<p:inputText value="#{bookBean.name}" a:placeholder="Ime knjige"></p:inputText><br/>
<p:inputText value="#{bookBean.description}" a:placeholder="Opis knjige"></p:inputText><br/>
<p:selectOneMenu value="#{bookBean.category}">
<f:selectItems value="#{categoryBean.allCategories}" var="c"
itemLabel="#{c.name}" itemValue="#{c.categoryId}"/>
</p:selectOneMenu>
<b/><b/>
<p:commandButton value="Dodaj knjigu" action="#{bookBean.addBook()}"/>
</h:form>
</p:layoutUnit>
}
答案 0 :(得分:0)
试试这个:
div
列出的项目名称将是类别名称,类别将分配给 bookBean.category ,这可以设置为图书类别并保留。
希望这有帮助。