- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Hibernate 时遇到问题,无法弄清楚我做错了什么。我有一个简单的表格:
CREATE TABLE users (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
firstName VARCHAR(30),
lastName VARCHAR(30),
email VARCHAR(50) NOT NULL UNIQUE,
login VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
然后是User实体类(自动生成):
package entity;
import ...
@Entity
@Table(name = "USERS")
@XmlRootElement
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long id;
@Size(max = 30)
@Column(name = "FIRSTNAME")
private String firstName;
@Size(max = 30)
@Column(name = "LASTNAME")
private String lastName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "EMAIL")
private String email;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "LOGIN")
private String login;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "PASSWORD")
private String password;
public User() {
}
public User(Long id) {
this.id = id;
}
public User(Long id, String email, String login, String password) {
this.id = id;
this.email = email;
this.login = login;
this.password = password;
}
getters and setters...
}
然后我创建一个新的 User 对象(不设置 ID 字段)并尝试保存在数据库中。此时我收到错误:
org.apache.derby.client.am.SqlException: Attempt to modify an identity column 'ID'.
at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:124)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:122)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:55)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3556)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:97)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:480)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:191)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:175)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:210)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:807)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:780)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:785)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:334)
at com.sun.proxy.$Proxy763.persist(Unknown Source)
at dao.UserDao.insert(UserDao.java:35)
at service.UserService.addUser(UserService.java:32)
at controller.LoginController.executeRegister(LoginController.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
我该如何解决这个问题?
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="LibraryPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/sample</non-jta-data-source>
<class>entity.User</class>
</persistence-unit>
</persistence>
调度程序-service.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<context:component-scan base-package="service, controller, dao" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="LibraryPU" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/Library" />
<property name="username" value="app" />
<property name="password" value="app" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
最佳答案
尝试以下操作
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.TABLE)
private Long id;
在您的情况下,数据库正在生成值,而不是 hibernate 。对于derby来说,你必须让hibernate委托(delegate)创建数据库。
关于java - Hibernate @GenerateValue 尝试修改标识列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34078634/
我正在 R 中使用 RecordLinkage 库。 我有一个包含 ID、姓名、电话、邮件的数据框 我的代码如下所示: ids = data$id pairs = compare.dedup(data
我目前正在构建一个新的 ASP.NET MVC 5 项目,我想在 9 月左右发布。我需要选择一个成员(member)系统,但我目前对我应该采取哪个方向感到很困惑。当前的 SimpleMembershi
我正在为 Brackets 定制一个大纲插件,它使用正则表达式来识别当前打开的文件的大纲。 我使用 regex101.com 创建了以下正则表达式(使用环视来确定该行以七个空格开头并以“SECTION
我已在表中将一列标记为“身份” create table Identitytest( number int identity(1,001) not null, value varch
我不知道那是字符串还是数组... char str4[100] = { 0 }; 那个代码是字符串? 如果是,它打印什么? 最佳答案 I dont know if that a string or a
我这里有一个场景,当用户想要重置密码时,系统必须通过电子邮件向用户发送一个随机生成的临时密码。我尝试将临时密码存储到数据库中的一个新列中,但我不确定这种方法是否有效。有些人建议使用 token,如下所
Vista 的现代 Windows 应用程序中有一个很好的功能。它是窗口标题中的图片。例如新的 skype (v4) 和 google chrome 都有它。 我在想它背后的技术是什么?如果你关闭 a
比较相同泛型类型的两个实例的最佳(最简洁和最佳)方法是什么,以便比较引用类型的身份(相同的对象,所以不是调用 Equals) 和 value 类型以获得值 equality。 目前我这样做: stat
我使用以下 C# 代码来获取处理器信息。如果我在虚拟机上运行我的应用程序,则管理类为空。我使用 Oracle VM VirtualBox 作为我的虚拟电脑 (Windows XP SP3) Syste
创建帐户后,Windows 帐户(本地、域、Active Directory)的 SID 是否会更改?如果是,在什么条件下。 最佳答案 是的,当您将帐户迁移到新域时,它会发生变化。 这就是您 AD 帐
我正在使用 Identity Server 4 并且我已经自定义了我的 ASP.NET Identity 用户,如下所示: public class ApplicationUser : Identit
我创建了一个 IIS 管理工具,旨在创建新应用程序、将它们分配到新的 AppPool,并为与该 AppPool 关联的身份添加所需的文件夹 ACL。根据this article ,每当创建新的应用程序
我使用 ASP.NET Identity .. 我想将 session 超时设置为无限制或最大值。我试过一些东西,但没有效果。注意:我使用共享主机。 谢谢你。 //web.config /
我有一台 Win 2008 R2 Enterprise 机器,它在几个网站上运行良好,每个网站都有自己的应用程序池。 我在向 IIS AppPool\A、IIS AppPool\B 等授予权限(使用
现有数据库模型(简化): 1 个用户可以加入 1 个或多个访问组。 1个AccessGroup可以有1个或多个AccessItens。 MSDN Says: When an identity is c
在具有单个表继承层次结构的 Hibernate/JPA 环境中使用 PostgreSQL 时,我看到了奇怪的行为。 首先是我的环境: PostgreSQL 8.3 Spring 2.5.6SEC01
是声明“一个类具有唯一标识”。是真是假? Java 中的对象有其唯一标识(至少通过它们的内存地址),但是类也有唯一标识吗?由于类不是对象,我对此感到困惑。或者是否需要实例化一个类(甚至可能)? 最佳答
我正在尝试通过将主要组件分解为单独的网络服务器来使用微服务架构来实现网络应用程序。我正在使用 ASP.NET Identity(仅电子邮件/用户名登录,无 Facebook 等)和“主”应用程序服务器
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How do you like your primary keys? 我知道使用 GUID 的好处,以及使用
我可以这样获取所有用户 var users = UserManager.Users.ToList(); 我能找到这样的角色 var role = db.Roles.SingleOrDefault(m
我是一名优秀的程序员,十分优秀!