Это весеннее приложение MVC, и оно является его частью.Когда я вставляю идентификатор категории в форму, это приводит меня к этой ошибке:
, но когда я удалил идентификатор категории ввод из формы, данные вставляются в базу данных.
Product.java
@Entity
@Table(name = "product", catalog = "flowershop")
public class Product implements java.io.Serializable {
private Integer id;
private Category categoryid;
private String name;
private Double price;
public Product() {
}
public Product(Category categoryid, String name, Double price) {
this.categoryid = categoryid;
this.name = name;
this.price = price;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "categoryid",foreignKey = @ForeignKey(name = "Product_categoryid_productid"))
public Category getCategoryid() {
return this.categoryid;
}
public void setCategoryid(Category categoryid) {
this.categoryid = categoryid;
}
@Column(name = "name", length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "price", precision = 22, scale = 0)
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
}
Category.java
@Entity
@Table(name="category", catalog="flowershop")
public class Category implements java.io.Serializable {
private Integer id;
private String name;
private Set<Product> products = new HashSet<Product>(0);
public Category() {
}
public Category(String name, Set<Product> products) {
this.name = name;
this.products = products;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="name", length=45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="categoryid")
public Set<Product> getProducts() {
return this.products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
ProductDAOImp.java
@Repository("productDAO")
public class ProductDAOImp implements ProductDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void newProduct(Product product) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(product);
tx.commit();
session.close();
}
ProductServiceImp.java
@Service("productService")
@Transactional
public class ProductServiceImp implements ProductService {
@Override
public void newProduct(Product product) {
productDAO.newProduct(product);
}
}
ProductController.java
@Controller
@Transactional
@RequestMapping("/product")
public class ProductController {
@Autowired
private CategoryService categoryService;
@Autowired
private ProductService productService;
@RequestMapping(value = "category/{id}", method = RequestMethod.GET)
public String category(@PathVariable("id") Integer id, ModelMap modelMap) {
Category category = categoryService.find(id);
Hibernate.initialize(category.getProducts());
modelMap.put("products", category.getProducts());
return "product.category";
}
@RequestMapping(value = {"newProduct"}, method = RequestMethod.GET)
public String newProduct(ModelMap modelMap) {
Product product = new Product();
modelMap.put("newProduct", product);
modelMap.put("title", "New Product");
return "product.newProduct";
}
@RequestMapping(value = {"newProduct"}, method = RequestMethod.POST)
public String newProduct(
@ModelAttribute("newProduct") Product product,
ModelMap modelMap) {
modelMap.put("newProduct", product);
try {
productService.newProduct(product);
return "product.newProduct";
} catch (Exception e) {
return "product.newProduct";
}
}
}
newProduct.jsp
<f:form method="POST" modelAttribute="newProduct" action="${pageContext.request.contextPath}/product/newProduct.html">
<div class="form_row">
<label class="contact"><strong>Name: </strong></label>
<input name="name" type="text" class="contact_input"/>
</div>
<div class="form_row">
<label class="contact"><strong>Price: </strong></label>
<input name="price" type="text" class="contact_input"/>
</div>
<div class="form_row">
<label class="contact"><strong>Category Id: </strong></label>
<input name="categoryid" type="text" class="contact_input"/>
</div>
<div class="form_row">
<input type="submit" class="register" value="register">
</div>
</f:form>