- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道以前可能有人问过这个问题,但我没有找到解决我的问题的方法。我有一个 varchar(20) 的categoryID 主键列,实体类由 id 属性映射到该主键列。添加数据时,它采用通过 UI 输入的 id 值,但在编辑其他字段并通过 UI 保存时,会抛出异常“IdentifierGenerationException:必须在调用 save() 之前手动分配此类的 ids:”
请找到下面的代码:
Category.java
-------------
package com.niit.cakecuisinebe.model;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Table
@Component
public class Category {
@Id
@Column(name="CATEGORYID")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
@Column(name="CATEGORYNAME")
private String name;
@Column(name="CATEGORYDESCRIPTION")
private String description;
}
CategoryDAOImpl
----------------
package com.niit.cakecuisinebe.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.niit.cakecuisinebe.model.Category;
@Repository("categoryDAO")
public class CategoryDAOImpl implements CategoryDAO{
@Autowired
private SessionFactory sessionFactory;
public CategoryDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public List<Category> list()
{
//Logger.debug("calling list");
@SuppressWarnings("unchecked")
List<Category> listCategory = (List<Category>)
sessionFactory.getCurrentSession()
.createCriteria(Category.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
// Logger.debug("calling list");
return listCategory;
}
@Transactional
public Category get(String id) {
// TODO Auto-generated method stub
String hql = "from Category where id=" + "'"+ id +"'";
// from category where id = '101'
Query query = sessionFactory.getCurrentSession().createQuery(hql);
List<Category> listCategory = (List<Category>) query.list();
if (listCategory != null && !listCategory.isEmpty()) {
return listCategory.get(0);
}
return null;
}
@Transactional
public void saveOrUpdate(Category category) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().saveOrUpdate(category);
}
@Transactional
public void delete(String id) {
// TODO Auto-generated method stub
Category category = new Category();
category.setId(id);
sessionFactory.getCurrentSession().delete(category);
}
@Transactional
public Category getByName(String name) {
// TODO Auto-generated method stub
String hql = "from Category where name=" + "'"+ name +"'";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<Category> listCategory = (List<Category>) query.list();
if (listCategory != null && !listCategory.isEmpty()) {
return listCategory.get(0);
}
return null;
}
}
CategoryController
--------------------
package com.niit;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.niit.cakecuisinebe.dao.CategoryDAO;
import com.niit.cakecuisinebe.model.*;
@Controller
public class CategoryController {
protected static Logger logger = Logger.getLogger("CategoryController");
@Autowired
private CategoryDAO categoryDao;
@RequestMapping(value = "/ViewCategory", method = RequestMethod.GET)
public String getCategory(Model model) {
logger.info("entering showAllGreetings");
model.addAttribute("category", new Category());
List<Category> categories = categoryDao.list();
if (!categories.isEmpty()) {
model.addAttribute("categorylist", categories);
}
return "Category";
}
@RequestMapping(value = "/addCategory", method = RequestMethod.POST)
public String addCategory(@ModelAttribute("category") Category category) {
logger.info("entering showAllGreetings");
categoryDao.saveOrUpdate(category);
return "redirect:/ViewCategory";
}
@RequestMapping(value = "edit/addCategory", method = RequestMethod.POST)
public String editCategory(@ModelAttribute("category") Category category) {
logger.info("entering showAllGreetings");
String id=category.getId();
category.setId(id);
categoryDao.saveOrUpdate(category);
return "redirect:/ViewCategory";
}
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
public String deleteCategory(@PathVariable("id") String id, ModelMap model) {
logger.info("entering showAllGreetings");
categoryDao.delete(id);
model.addAttribute("{msg}", "Successfully Deleted");
return "redirect:/ViewCategory";
}
@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
public String showEditCategory(@PathVariable("id") String id, Model model) {
logger.info("entering showAllGreetings");
model.addAttribute("category", this.categoryDao.get(id));
model.addAttribute("categorylist", categoryDao.list());
return "Category";
}
}
<div >
<spring:form method="POST" action="addCategory" modelAttribute="category" align="center">
<table cellpadding="5" cellspacing="5" style="height=45px; width=45px; background-color: pink; padding=10px">
<tr>
<c:choose>
<c:when test="${!empty category.id}">
<td><spring:label path="id"><springtags:message text="CategoryID"></springtags:message></spring:label></td>
<td><spring:input path="id" disabled="true" readonly="true" /></td>
</c:when>
<c:otherwise>
<td><spring:label path="id"><springtags:message text="CategoryID"></springtags:message></spring:label></td>
<td><spring:input path="id" /></td>
</c:otherwise>
</c:choose>
</tr>
<tr>
<td> Category Name:</td>
<td><spring:input path="name" /></td>
</tr>
<tr>
<td> Category Description:</td>
<td><spring:input path="description" /></td>
</tr>
<tr>
<c:if test="${!empty category.id}">
<td> <input type="submit"
value="<springtags:message text="Edit Category"/>" />
</c:if>
</td>
<td><c:if test="${empty category.id}">
<input type="submit" value="<springtags:message text="Add Category"/>" />
</c:if>
</td>
</tr>
</table>
</spring:form>
</div>
<div class="container">
<table class="table table-striped table-bordered table-hover table-condensed">
<caption><h2>Categories</h2></caption>
<thead>
<tr>
<th>Category ID</th>
<th>Category Name</th>
<th>Category Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<c:if test="${not empty categorylist}" >
<tbody>
<c:forEach items="${categorylist}" var="category">
<tr>
<td>${category.id}</td>
<td>${category.name}</td>
<td>${category.description} </td>
<td><a href="edit/${category.id}">Edit</a> </td>
<td><a href="delete/${category.id}">Delete</a> </td>
</tr>
</c:forEach>
</tbody>
</c:if>
<c:if test="${empty categorylist}" >
There are no category yet.
</c:if>
</tr>
</table>
</div>
<%@ include file="footer.jsp"%>
</body>
</html>
请帮忙解决过去两天的问题
最佳答案
我不太喜欢字符串类型的 ids。你应该换一个长id。因此,您可以使用一些排序内容将 id 的生成委托(delegate)给数据库。
如果您希望实际列中的数据是唯一的,只需创建另一个具有唯一约束的字段即可。
关于java - org.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save():,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38620878/
我有两个如下所示的实体 @Data @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "foo") @Audited @AuditO
我使用 MySQL、Hibernate、JPA 和 Spring,我的问题是我无法一对一关联更新子表;我正在使用外国策略。 当我使用 saveOrUpdate 并调用第一次方法时,我可以将实体保存到数
当我运行我的主类 (Runner) 程序时,出现以下异常: org.hibernate.id.IdentifierGenerationException: attempted to assign id
为了快速生产,我正在尝试使用 JBoss Tools 4.1 Hibernate 插件。但我在第一回合遇到了异常(exception)。 首先物理数据库表如下: describe Teams ;
我有以下表格: @Entity @Table(name = "CUSTOMER") public class Customers implements Serializable { priva
如何在BetRecordAgEBR中传递和设置ID?请帮助我:) 提前致谢。这是我遇到的唯一问题。 BetRecordAgEBR 类适用于使用 jpa 的数据库。 if ("GR".equals(da
在一种情况下,我无法将我的数据库域模型映射到程序实体,其中该实体本质上是一个连接表(一个句点),它结合了另外两个实体(一个时间段和一天)。另一个实体(一节课)然后引用这个周期实体,确定它何时发生。 当
我有两个实体,名为 Customer 和他的 Biling Address。关系是一对一的。每个客户都有一个账单地址。我想在保留客户时自动保留帐单地址。客户 ID 是客户实体的主键,也是地址实体中的主
我正在尝试将数据保存到数据库(保存到两个表中,使用一对一映射)。 我正在使用 Hibernate、Struts2 和 MySQL 数据库。 保存数据时显示以下异常: org.hibernate.id.
我正在尝试使用“一对一”共享主键映射实体,我收到了标题中提到的错误,我已经在这里检查了以前的帖子,但没有解决我的问题。下面是相关代码: 这是在 Localsession 工厂中设置的 hibernat
当我尝试使用 Spring Boot 2.1.6.RELEASE 和 Spring Data JPA 将用户对象保存到数据库时遇到问题。 用户对象和详细信息对象具有双向一对一关系。 明细的id与用户的
我知道以前可能有人问过这个问题,但我没有找到解决我的问题的方法。我有一个 varchar(20) 的categoryID 主键列,实体类由 id 属性映射到该主键列。添加数据时,它采用通过 UI 输入
虽然我已经手动设置了 ID,但我收到了上述错误,但我在这里没有使用自动生成的 key 。当我设置 key 并将对象传递给 entityManager.persist(obj); 它给出了上面的错误。任
我正在 Hibernate 中编写多对一程序,当我在 DAO 中编写以下代码时: sessionFactory = HibernateUtility.createSessionFactory();
我在表中有一个复合主键(表名称:billingdetails,主键:billId 和 productId)。当我调用 ../billingdetails/save 时,它会抛出 org.hiberna
我正在 eclipse 中开发 spring 4.0 和 hibernate 4.0.1 项目,并使用 oracle 作为数据库。如果我在数据库中进行一些更改,那么更改将不会受到 eclipse 的影
我多次尝试使用下面的代码在表中插入 id 值,但它总是抛出 org.hibernate.id.IdentifierGenerationException: 此类的 ids 必须在调用 save() 之
我正在 Java 中构建一个休息 Web 服务,使用 json 输入 hibernate 进行数据库连接,对于数据库,我使用 MS SQL Server 作为数据库。虽然我的数据库表 ID 列设置为自
我是一名优秀的程序员,十分优秀!