Hibernate Mapping思考错误

时间:2017-04-18 06:50:09

标签: java hibernate hibernate-mapping

我有2个数据库表。首先进行交易。每笔交易都有一个ID。这个ID也可以在我的第二张表中找到。第二张表包含费用,但我的第一张表中没有关于收费的信息。现在我必须通过hibernate映射获得与1交易相关的所有费用,但我不想在两个方向上都有它。

贸易类:

public class Trade {
    @Id
    @Column(name = "ID")
    private Long tradeTag;

    @ElementCollection
    @CollectionTable(name = "VIE_CHARGES", joinColumns = 
    @JoinColumn(name="TRADE"))
    private List<Charge> charges;
}

收费等级:

public class Charge implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    private String chargeType;
    @Id
    @Column(name="TRADE")
    private Long tradeTag;
}

在收费课程中,我不想持有有关交易的任何信息,但在交易类别中,我会列出所有交易费用。我很乐意在这里找到解决方案,因为我真的不知道该怎么做。

查看收费:

create view VIE_CHARGES as 
select CHRG_TYPE AS CHARGETYPE
,TRAD_TAG AS TRADE
from TRADE_CHARGE

贸易观点:

create VIEW VIE_TRADE AS 
select TRAD_TAG as ID
from TRADE 

错误是:

  

org.hibernate.exception.SQLGrammarException:无法解压缩   ResultSet at   org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)     在   org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)     在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)     在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)     在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)     在org.hibernate.loader.Loader.getResultSet(Loader.java:2123)at   org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1911)     在   org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)     在org.hibernate.loader.Loader.doQuery(Loader.java:932)at   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)     在   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:319)     在org.hibernate.loader.Loader.loadCollection(Loader.java:2327)at   org.hibernate.loader.collection.plan.LegacyBatchingCollectionInitializerBuilder $ LegacyBatchingCollectionInitializer.initialize(LegacyBatchingCollectionInitializerBuilder.java:88)     在   org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)     在   org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)     在   org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2150)     在   org.hibernate.collection.internal.AbstractPersistentCollection $ 4.doWork(AbstractPersistentCollection.java:567)     在   org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:249)     在   org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:563)     在   org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132)     在   org.hibernate.collection.internal.AbstractPersistentCollection $ 1.doWork(AbstractPersistentCollection.java:161)     在   org.hibernate.collection.internal.AbstractPersistentCollection $ 1.doWork(AbstractPersistentCollection.java:146)     在   org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:249)     在   org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145)     在   org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)     在   com.mainfirst.emma.archive.trade.rims.TestDataBaseconnection.testTradeDao(TestDataBaseconnection.java:28)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:com.microsoft.sqlserver.jdbc.SQLServerException:无效   列名称&#39; charges_TRADE&#39; 。在   com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)     在   com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)     在   com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)     在   com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement $ PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)     在   com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)     在   com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)     在   com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)     在   com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)     在   com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)     在   org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)     在   org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)     在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)     ......还有50个

3 个答案:

答案 0 :(得分:1)

在hibernate中,这是使用双向@OneToMany并保持关系双方同步的最佳方法。像这样的Smth:

@Entity(name = "Person")
public class Person {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
    private Set<Phone> phones=new HashSet<Phone>();
//getters and setters
public void addPhone(Phone phone) {
        phone.setPerson(this);
        phones.add(phone);
    }

    public void removePhone(Phone phone) {
        phone.setPerson(null);
        phones.remove(phone);
    }
}

和电话实体:

@Entity(name = "Phone")
public class Phone {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "`number`")
    private String number;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id", nullable = false)
    private Person person;

阅读文档https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#associations-one-to-many

答案 1 :(得分:1)

如果收费对象不需要自己的生命周期(没有自己的标识符,没有多个交易引用,不应该单独存在,应该用它进行交易),你可以将其映射为{{1}值类型(@ElementCollection)。

映射表和id列名称可以由@Embeddable@CollectionTable注释设置。它的默认值为&#34; trade_charges&#34;和&#34; trade_id&#34;。

@JoinColumn注释只是一点性能提升,只有当你面对集合的很多变化时才需要它。它为trade_charges表添加了一个索引列,以标识更新/删除语句中的行。

例如贸易:

@OrderColumn

收费:

@Entity
public class Trade {

    @Id
    @Column(name = "ID")
    private Long tradeTag;

    @ElementCollection
    @CollectionTable(name = "charges", joinColumns = @JoinColumn(name="ID"))
    @OrderColumn
    private List<Charge> charges;

}

答案 2 :(得分:0)

所以在@OneToMany的文档中有一个例子,我认为它尽可能符合我的需求。

我现在正在处理的解决方案:

@Embeddable
public class Charge {

    private String chargeType;

}

如果有人有更好的想法随时与我分享