1

Hibernate で JPA を使用して Postgres データベースに接続していますが、エンティティをデータベースに永続化できません。

データベースには次のものがあります。

CREATE SEQUENCE "MODEL_C_MODEL_seq"
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 93
  CACHE 1;
ALTER TABLE "MODEL_C_MODEL_seq"

CREATE TABLE "MODEL"
(
  "C_USER" character varying(10) NOT NULL,
  "C_MODEL" serial NOT NULL,
  "TP_MODEL" character(5) NOT NULL,
  "NM_MODEL" character varying(20) NOT NULL,
  CONSTRAINT "MODEL_PK" PRIMARY KEY ("C_MODEL" ),
  CONSTRAINT "MODEL_CUSER_FK" FOREIGN KEY ("C_USER")
      REFERENCES "USER" ("C_USER") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "MODEL_TPMODEL_FK" FOREIGN KEY ("TP_MODEL")
      REFERENCES "ADM_TYPES" ("C_TYPE") MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "MODEL_CUSER_NMNAME_UNIQUE" UNIQUE ("C_USER" , "NM_MODEL" )
)
WITH (
  OIDS=FALSE
);

私のエンティティは:

public class Model implements Serializable {
    private static final long serialVersionUID = 1L;

    **@Id
    @SequenceGenerator(name="modelPKgen", sequenceName="MODEL_C_MODEL_seq")
    @GeneratedValue(generator="modelPKgen", strategy= GenerationType.IDENTITY)
    @Column(name = "\"C_MODEL\"", unique=true, insertable=false)    
    private int cModel;**

    @OneToMany(cascade = CascadeType.ALL,  fetch= FetchType.EAGER)//, mappedBy = "model", fetch= FetchType.EAGER)
    @JoinColumn(name= "`C_MODEL`")
    private Collection<State> stateCollection;

    @JoinColumn(name = "`C_USER`", referencedColumnName ="`C_USER`", updatable = false)
    @ManyToOne(optional = false)
    private User user;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "\"NM_MODEL\"")
    private String nmModel;     

    @JoinColumn(name = "\"TP_MODEL\"")
    @ManyToOne(optional = false)
    private AdmTypes admTypes;
...

多くの投稿をチェックインすると、フィールドcModelに正しい注釈があるようです。

私は次のことを行うクラスを持っています:

Model dbModel = new Model(user.getCUser(), simModel.getName());
        AdmTypes dbAdm = new AdmTypes(simModel.getType());
        dbModel.setAdmTypes(dbAdm);

        **em.persist(dbModel);**
        em.flush();

実行するem.persistと、次のエラーで例外が発生します。

Caused by: org.hibernate.exception.SQLGrammarException: The column name "C_MODEL" was not found in this ResultSet.

em.persist で Hibernate によって行われた挿入ステートメントを確認すると、次のように表示されます。

insert 
    into
        "MODEL"
        ("TP_MODEL", "NM_MODEL", "C_USER") 
    values
        (?, ?, ?)

C_MODELその文はpostgresで実行されるため、これは完璧です(新しく生成されたIDを持つ新しいレコードを挿入します)。

誰がそれが何であるか知っていますか?

更新 (完全なスタック トレース):

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: The column name "C_MODEL" was not found in this ResultSet.
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1367)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1295)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1301)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:866)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:269)
    at com.librethinking.simmodsys.persistence.adapters.ModelConverter.SIMModelToModel(ModelConverter.java:65)
    at com.librethinking.simmodsys.ejb.ModelBean.saveSIMModel(ModelBean.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy186.saveSIMModel(Unknown Source)
    at com.librethinking.simmodsys.ejb.__EJB31_Generated__ModelBean__Intf____Bean__.saveSIMModel(Unknown Source)
    at com.librethinking.simmodsys.web.ModelsController.saveModel(ModelsController.java:366)
    at com.librethinking.simmodsys.web.ModelsController.processRequest(ModelsController.java:104)
    at com.librethinking.simmodsys.web.ModelsController.doPost(ModelsController.java:181)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.hibernate.exception.SQLGrammarException: The column name "C_MODEL" was not found in this ResultSet.
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:108)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy189.getInt(Unknown Source)
    at org.hibernate.id.IdentifierGeneratorHelper.get(IdentifierGeneratorHelper.java:152)
    at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:92)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:100)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2767)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3278)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:844)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:819)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:823)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:860)
    ... 58 more
Caused by: org.postgresql.util.PSQLException: The column name "C_MODEL" was not found in this ResultSet.
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.findColumn(AbstractJdbc2ResultSet.java:2550)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2414)
    at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:104)
    ... 83 more
4

1 に答える 1

1

いくつかのことが飛び出します:

  1. シーケンス ジェネレーターを作成し、id 世代をGenerationType.IDENTITY. GenerationType.SEQUENCEまたはを試してみGenerationType.AUTOます。Hibernate のドキュメントでは、ID は DB2、MySQL、MS SQL Server、Sybase、および HypersonicSQL 用であると書かれているため、ID が機能した場合は驚くでしょう。

  2. あなたの識別子の注釈insertable=falseから属性を取り除きます。@Columnシーケンス生成属性に基づいて、Hibernate がシーケンス値を選択して挿入すると仮定します。SELECT currval(pg_get_serial_sequence(...))これは、insertable が true の場合にのみ機能します。

  3. sequenceName="\"MODEL_C_MODEL_seq\"" を sequenceName="MODEL_C_MODEL_seq" に変更します。

于 2012-10-03T13:38:34.623 に答える