为了清理和重构一些代码,我创建了一个新工作区和新项目。我的项目设置如下:一个核心项目(Javaproject使用Maven进行依赖管理)和两个动态Web项目(JSF),它们共享来自核心项目的代码(类似于this SO-Post的设置)。我正在使用带有Hibernate和Primefaces的Wildfly 10。 到目前为止,我所做的是将旧的凌乱项目中的基本类复制到新的核心项目,以便启动和运行应用程序。代码没有被触及,它在我的旧设置中工作正常。应用程序启动正常并导航到登录页面,但每次我按下登录按钮时都会收到以下异常:
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: increment
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:641)
at org.hibernate.cfg.AnnotationBinder.processId(AnnotationBinder.java:2346)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2251)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:911)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:738)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
... 68 more
org.hibernate.AnnotationException: Unknown Id.generator: increment
实际上应该不会发生,因为所有内容都注释正常并且一直正常工作。
有趣的是,DB-table也不会像实际应该那样自动创建。起初我认为这可能是由于碰撞了JPA之类的依赖项,因为在Web项目的已部署WEB-INF\lib
文件夹中,我发现了一堆不必要的jars,如jboss *和hibernate *等。遗憾的是删除它们并没有解决问题
示范模型类:
@Entity
@Table(name = "user")
@NamedQuery(name = "User.findUserByEmail", query = "select u from User u where u.email = :email")
public class User implements Serializable
{
public static final String FIND_BY_EMAIL = "User.findUserByEmail";
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "PK_user_id")
private int id;
@OneToMany
@JoinColumn(name = "FK_user_id")
private List<ArticleList> articleLists;
@Column(name = "user_email")
private String email;
@Column(name = "user_first_name")
private String firstName;
@Column(name = "user_hashed_password")
private byte[] hashedPassword;
@Column(name = "user_last_name")
private String lastName;
@Enumerated(EnumType.STRING)
@Column(name = "user_role")
private Role role;
@Column(name = "user_salt")
private byte[] salt;
//getters and setters
}
我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="toyjoPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>de.jb.toyjo.model.Article</class>
<class>de.jb.toyjo.model.ArticleList</class>
<class>de.jb.toyjo.model.Category</class>
<class>de.jb.toyjo.model.TextModule</class>
<class>de.jb.toyjo.model.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/toyjodb_tcs?autoReconnect=true" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.shutdown" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
</persistence>
为什么会发生这种情况的任何线索?提前谢谢
编辑:我试过@Rohit Gaikwad提到但现在收到此错误:
10:47:36,006 INFO [stdout] (default task-35) Error while running query: org.hibernate.type.SerializationException: could not deserialize
10:47:36,007 ERROR [stderr] (default task-35) javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
10:47:36,008 ERROR [stderr] (default task-35) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
10:47:36,008 ERROR [stderr] (default task-35) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
10:47:36,009 ERROR [stderr] (default task-35) at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:560)
10:47:36,009 ERROR [stderr] (default task-35) at de.jb.toyjo.dao.GenericDAO.findOneResult(GenericDAO.java:130)
10:47:36,010 ERROR [stderr] (default task-35) at de.jb.toyjo.dao.UserDAO.findUserByEmail(UserDAO.java:23)
10:47:36,010 ERROR [stderr] (default task-35) at de.jb.toyjo.facade.UserFacade.isValidLogin(UserFacade.java:14)
10:47:36,011 ERROR [stderr] (default task-35) at de.jb.toyjo.bean.LoginBean.login(LoginBean.java:24)
10:47:36,011 ERROR [stderr] (default task-35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:47:36,011 ERROR [stderr] (default task-35) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
10:47:36,012 ERROR [stderr] (default task-35) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
10:47:36,012 ERROR [stderr] (default task-35) at java.lang.reflect.Method.invoke(Method.java:498)
10:47:36,013 ERROR [stderr] (default task-35) at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)
10:47:36,013 ERROR [stderr] (default task-35) at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
10:47:36,014 ERROR [stderr] (default task-35) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
10:47:36,014 ERROR [stderr] (default task-35) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:47:36,015 ERROR [stderr] (default task-35) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:47:36,015 ERROR [stderr] (default task-35) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
10:47:36,015 ERROR [stderr] (default task-35) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
10:47:36,016 ERROR [stderr] (default task-35) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
10:47:36,016 ERROR [stderr] (default task-35) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
10:47:36,017 ERROR [stderr] (default task-35) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
10:47:36,017 ERROR [stderr] (default task-35) at javax.faces.component.UICommand.broadcast(UICommand.java:315)
10:47:36,017 ERROR [stderr] (default task-35) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
10:47:36,018 ERROR [stderr] (default task-35) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
10:47:36,018 ERROR [stderr] (default task-35) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
10:47:36,019 ERROR [stderr] (default task-35) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
10:47:36,019 ERROR [stderr] (default task-35) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
10:47:36,019 ERROR [stderr] (default task-35) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
10:47:36,020 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
10:47:36,020 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
10:47:36,020 ERROR [stderr] (default task-35) at de.jb.toyjo.filter.LoginCheckFilter.doFilter(LoginCheckFilter.java:84)
10:47:36,021 ERROR [stderr] (default task-35) at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
10:47:36,021 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
10:47:36,021 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
10:47:36,022 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
10:47:36,022 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
10:47:36,023 ERROR [stderr] (default task-35) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
10:47:36,023 ERROR [stderr] (default task-35) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:47:36,023 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
10:47:36,024 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
10:47:36,024 ERROR [stderr] (default task-35) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:47:36,025 ERROR [stderr] (default task-35) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
10:47:36,025 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
10:47:36,026 ERROR [stderr] (default task-35) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
10:47:36,026 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
10:47:36,026 ERROR [stderr] (default task-35) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
10:47:36,027 ERROR [stderr] (default task-35) at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
10:47:36,027 ERROR [stderr] (default task-35) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:47:36,028 ERROR [stderr] (default task-35) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
10:47:36,028 ERROR [stderr] (default task-35) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:47:36,029 ERROR [stderr] (default task-35) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:47:36,029 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
10:47:36,029 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
10:47:36,030 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
10:47:36,030 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
10:47:36,031 ERROR [stderr] (default task-35) at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
10:47:36,031 ERROR [stderr] (default task-35) at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
10:47:36,032 ERROR [stderr] (default task-35) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
10:47:36,032 ERROR [stderr] (default task-35) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
10:47:36,033 ERROR [stderr] (default task-35) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
10:47:36,033 ERROR [stderr] (default task-35) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
10:47:36,034 ERROR [stderr] (default task-35) at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
10:47:36,034 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
10:47:36,034 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
10:47:36,035 ERROR [stderr] (default task-35) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
10:47:36,035 ERROR [stderr] (default task-35) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
10:47:36,036 ERROR [stderr] (default task-35) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
10:47:36,036 ERROR [stderr] (default task-35) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
10:47:36,036 ERROR [stderr] (default task-35) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
10:47:36,037 ERROR [stderr] (default task-35) at java.lang.Thread.run(Thread.java:745)
10:47:36,038 ERROR [stderr] (default task-35) Caused by: org.hibernate.type.SerializationException: could not deserialize
10:47:36,038 ERROR [stderr] (default task-35) at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:243)
10:47:36,039 ERROR [stderr] (default task-35) at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
10:47:36,039 ERROR [stderr] (default task-35) at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
10:47:36,040 ERROR [stderr] (default task-35) at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
10:47:36,040 ERROR [stderr] (default task-35) at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
10:47:36,040 ERROR [stderr] (default task-35) at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:60)
10:47:36,041 ERROR [stderr] (default task-35) at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
10:47:36,041 ERROR [stderr] (default task-35) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
10:47:36,041 ERROR [stderr] (default task-35) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234)
10:47:36,042 ERROR [stderr] (default task-35) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224)
10:47:36,042 ERROR [stderr] (default task-35) at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300)
10:47:36,043 ERROR [stderr] (default task-35) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2738)
10:47:36,043 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729)
10:47:36,044 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655)
10:47:36,045 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.getRow(Loader.java:1544)
10:47:36,045 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
10:47:36,046 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
10:47:36,046 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.doQuery(Loader.java:930)
10:47:36,047 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
10:47:36,047 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.doList(Loader.java:2617)
10:47:36,048 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.doList(Loader.java:2600)
10:47:36,049 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
10:47:36,049 ERROR [stderr] (default task-35) at org.hibernate.loader.Loader.list(Loader.java:2424)
10:47:36,050 ERROR [stderr] (default task-35) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
10:47:36,050 ERROR [stderr] (default task-35) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
10:47:36,051 ERROR [stderr] (default task-35) at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
10:47:36,051 ERROR [stderr] (default task-35) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326)
10:47:36,052 ERROR [stderr] (default task-35) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
10:47:36,052 ERROR [stderr] (default task-35) at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
10:47:36,052 ERROR [stderr] (default task-35) at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529)
10:47:36,053 ERROR [stderr] (default task-35) ... 67 more
10:47:36,054 ERROR [stderr] (default task-35) Caused by: java.io.StreamCorruptedException: invalid stream header: 41444D49
10:47:36,055 ERROR [stderr] (default task-35) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:808)
10:47:36,055 ERROR [stderr] (default task-35) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
10:47:36,055 ERROR [stderr] (default task-35) at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:309)
10:47:36,056 ERROR [stderr] (default task-35) at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:299)
10:47:36,056 ERROR [stderr] (default task-35) at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:218)
10:47:36,057 ERROR [stderr] (default task-35) ... 96 more
答案 0 :(得分:0)
byte[]
到String
转换。 SHA生成原始字节数组,无论编码如何,都无法保证任意byte[]
将产生有效的String
。因此,您的代码只能通过意外工作。byte[]
使用String
hashedPassword
和salt
。如果这样做,那么问题就是将byte[]
映射到hibernate。 注意:保持使用increment
生成主键的代码。