0

我很高兴参与这个网站。我是 Stack Overflow 的新手,但总有第一次 :)。

目前我正在使用 Spring Data 1.3.0 和 Hibernate 4.1.9,使用 JPA 2.0 over Java 生成基于存储库的查询,数据库是 SQL Server 2008。我需要使用两个表进行插入操作:A模式表称为 PATTERN_TABLE。该表被另一个称为 CHILD_TABLE 的表引用,它们的关系是一对多的。我针对此要求开发解决方案的指南是以下链接:Hibernate one-to-many mapping using annotations,并且我正在尝试将其调整为弹簧数据支持(存储库管理)。但是它失败了,因为 Hibernate 对于插入,尝试查找 CHILD_TABLE 表(在数据库中)和映射实体中不存在的列。我不知道为什么 Hibernate 会试图找到这个列名,我认为它是一个别名(称为 patternTable_CODIGO)。该错误由以下日志注册表定义:

Hibernate: insert into GDA.dbo.PATTERN_TABLE (COLUMN_A, COLUMN_B, COLUMN_C, COLUMN_D) values (?, ?, ?, ?)
(...)
Hibernate: insert into GDA.dbo.CHILD_TABLE (COLUMN_CHILD_A, COLUMN_CHILD_B, patternTable_CODIGO) values (?, ?, ?) --->patternTable_CODIGO doesn't exists in the database!!!
2013-09-19 18:05:12,967 [Thread-18] [SqlExceptionHelper:logExceptions] WARN  SQL Error: 207, SQLState: 42S22
2013-09-19 18:05:12,967 [Thread-18] [SqlExceptionHelper:logExceptions] ERROR [FMWGEN][SQLServer JDBC Driver][SQLServer]Invalid column name 'patternTable_CODIGO'.
2013-09-19 18:05:12,967 [Thread-18] [SqlExceptionHelper:logExceptions] WARN  SQL Error: 8180, SQLState: HY000
2013-09-19 18:05:12,968 [Thread-18] [SqlExceptionHelper:logExceptions] ERROR [FMWGEN]        [SQLServer JDBC Driver][SQLServer]Statement(s) could not be prepared.
org.springframework.dao.InvalidDataAccessResourceUsageException: [FMWGEN][SQLServer JDBC Driver][SQLServer]Invalid column name 'patternTable_CODIGO'.; 
SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: [FMWGEN][SQLServer JDBC Driver][SQLServer]Invalid column name 'patternTable_CODIGO'.
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:635)
   at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106)
   at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
   (...)
   at sun.proxy.$Proxy274.save(Unknown Source)
   at co.com.myproject.impl.PatternTableServiceImpl.guardar(PatternTableServiceImpl.java:25)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   (...)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at sun.proxy.$Proxy275.guardar(Unknown Source)
   at co.com.myproject.principal.GenerarPlano.inicio(GenerarPlano.java:102)
Caused by: org.hibernate.exception.SQLGrammarException: [FMWGEN][SQLServer JDBC Driver][SQLServer]Invalid column name 'patternTable_CODIGO'.
   at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
   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.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
   at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
   at sun.proxy.$Proxy264.executeUpdate(Unknown Source)
   at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2936)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447)
   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.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71)
   at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:913)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:895)
   at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
   at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:383)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:412)
   at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:353)
   at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:329)
   at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
   at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
   at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:448)
   at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:220)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154)
   at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
   at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:903)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:887)
   at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:891)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)
   at sun.reflect.GeneratedMethodAccessor280.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
   at sun.proxy.$Proxy256.merge(Unknown Source)
   at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:353)
   at sun.reflect.GeneratedMethodAccessor278.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)
   at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
   ... 33 more

这是PatternTable实体:

package co.com.myproject.persistencia;
// ... libraries
import javax.persistence.*;

@Entity
@Table(name = "PATTERN_TABLE", schema = "dbo", catalog = "GDA")
public class PatternTable implements Serializable {
    private static final long serialVersionUID = 1L;
    private int codigo;

    // ... a lot of mapped fields...

    private Set<ChildTable> childTable = new HashSet<ChildTable>(0);

    public PatternTable() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CODIGO")
    public int getCodigo() {
            return codigo;
    }

    public void setCodigo(int codigo) {
            this.codigo = codigo;
    }

    // ... a lot of getters and setters...

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="CODIGO_PATTERN_TABLE")
    public Set<ChildTable> getChildTable() {
            return childTable;
    }

    public void setChildTable(Set<ChildTable> childTable) {
            this.childTable = childTable;
    }
}

这是ChildTable实体

package co.com.myproject.persistencia;
//... libs
@Entity
@Table(name = "CHILD_TABLE", schema = "dbo", catalog = "GDA")
public class ChildTable implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private int codigo;
    private PatternTable patternTable;
    //... more attribures

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CODIGO", unique = true, nullable = false)
    public int getCodigo() {
            return codigo;
    }

    public void setCodigo(int codigo) {
    this.codigo = codigo;
}

@ManyToOne
public PatternTable getPatternTable() {
    return patternTable;
}

public void setPatternTable(PatternTable patternTable) {
    this.patternTable = patternTable;
}

// ... more getters and setters
}

这是我的存储库接口PatternTable

@Transactional(readOnly = true)
public interface PatternTableRepository extends JpaRepository<PatternTable, Integer> {
    List<PatternTable> findByPatternParameter(String patternParameter);
    // ... more code next
}

我的服务方法定义为:

public void guardar(PatternTable patternTable) {
    patternTableRepository.save(patternTable);
}

并且patternTable' 对象childTable通过以下方式收集一个或多个:

childTable = new ChildTable();
//...set attributes of childTable...
patternTable.getChildTable().add(childTable); 

如果我的英语不流利,我很抱歉。我会尝试找到一个解决方案,但如果你能帮助我,我将不胜感激。非常感谢!:)

4

1 回答 1

0

嗯......我不能为这个麻烦开发出我理想的解决方案,但我可以开发一个非常快速和简单的解决方案。这工作如下:

  1. 在实体 beanPatternTable中编写一些更改。ChildTableChildTable@ManyToOne 注释之后,添加 FetchType 和 JoinColumn,如下所示:

    //Entity definition and getters / setters...        
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CODIGO_PATTERN_TABLE", nullable = false)
    public PatternTable getPatternTable() {
        return patternTable;
    }
    //More getters and setters...
    

    此外,在PatternTable实体 bean 中,修改 @OneToMany 关系如下:

    //Entity definition and getters / setters... 
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "patternTable")
    public Set<ChildTable> getChildTable() {
        return childTable;
    }
    //More getters and setters...
    
  2. 修改实体 bean 的保存方法。第一次,我保存了没有 Set of s的PatternTable实体 bean 。ChildTable之后,我将ChildTables' 集与它们各自的集合相关联,PatternTable并创建了一个新的存储库ChildTable来保存这些。

非常感谢您查看这个问题,并为我糟糕的英语感到抱歉!:)

于 2013-10-02T18:01:18.907 回答