Hibernate在SINGLE_TABLE吸气策略中分配错误的类

时间:2015-07-03 13:08:18

标签: java hibernate mapping eager-loading single-table-inheritance

我是hibernate的新手,我正在玩继承策略。 我为名为Components的对象建模评分系统。您可以分别在TextScoreNumericScore中拥有TextScoringCriterionNumericScoringCriterion。我决定对分数和crteria使用SINGLE_TABLE继承策略。

简化下面的课程。

@Entity
@Table(name = "components", schema = "public")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Component {

 @Id
 @SequenceGenerator(name="component_seq", sequenceName="component_id_seq")
 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="component_seq")
 @Column(name = "component_id", nullable = false, insertable = true, updatable = true)
 private long componentId;

 //RELATIONS
 @OneToMany(mappedBy = "component", fetch=FetchType.EAGER)
 private Set<Score> scores = new HashSet<Score>();
}


@Entity
@Table(name = "programs", schema = "public")
public class Program extends Component  {
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@DiscriminatorOptions(force = true)
@Table(name = "scores", schema = "public")
public abstract class Score {
    //ID
    @EmbeddedId
    protected ScorePK scoresPK;

    //BASICS
    @Basic
    @Column(name = "motivation", nullable = true, insertable = true, updatable = true, length = 2147483647)
    private String motivation;

    //RELATIONS
    @ManyToOne(optional = false)
    @JoinColumn(name = "component_id", referencedColumnName = "component_id", insertable = false, updatable = false)
    private Component component;
}

@Entity
@DiscriminatorValue("N")
public class NumericScore extends Score{
    //BASICS
    @Basic
    @Column(name = "score", nullable = true, insertable = true, updatable = true, precision = 2)
    private BigDecimal score;

    @ManyToOne(optional = false)
    @JoinColumn(name = "scoring_criterion_id", referencedColumnName = "scoring_criterion_id", insertable = false, updatable = false)
    private NumericScoringCriterion scoringCriterion;
}

@Entity
@DiscriminatorValue("T")
public class TextScore extends Score{
    //BASICS
    @Basic
    @Column(name = "answer", nullable = true, insertable = true, updatable = true, length = 150)
    private String answer;


    @ManyToOne(optional = false)
    @JoinColumn(name = "scoring_criterion_id", referencedColumnName = "scoring_criterion_id", insertable = false, updatable = false)
    private TextScoringCriterion scoringCriterion;
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@DiscriminatorOptions(force = true)
@Table(name = "scoring_criteria", schema = "public")
public abstract class ScoringCriterion {
    //ID
    @Id
    @SequenceGenerator(name="scoring_criterion_seq", sequenceName="scoring_criterion_id_seq")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="scoring_criterion_seq")
    @Column(name = "scoring_criterion_id", nullable = false, insertable = true, updatable = true)
    private long scoringCriterionId;

    //BASICS
    @Basic
    @Column(name = "code", nullable = false, insertable = true, updatable = true, length = 8)
    private String code;
    @Basic
    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 50)
    private String name;
    @Basic
    @Column(name = "description", nullable = true, insertable = true, updatable = true, length = 2147483647)
    private String description;
}


@Entity
@DiscriminatorValue("N")
public class NumericScoringCriterion extends ScoringCriterion {
    @Basic
    @Enumerated(EnumType.STRING)
    @Column(name = "best_is", nullable = true, insertable = true, updatable = true, length = 3)
    private SuperiorityStrategy bestIs;

}

@Entity
@DiscriminatorValue("T")
public class TextScoringCriterion extends ScoringCriterion {
    @Basic
    @Column(name = "question", nullable = true, insertable = true, updatable = true, length = 150)
    private String question;
}

我编写的测试NumericScoringCriterionTextScoringCriterion正确插入,更新,由ID获取并删除。 然后我为NumericScore编写了测试。它已正确添加,更新和删除。 但是当我为TextScore编写测试时,当我尝试从DB获取程序时,我得到了一个奇怪的错误。

2015-07-03_14:27:15.714 DEBUG o.h.engine.internal.TwoPhaseLoad - Resolving associations for [model.categorization.TextScore#component[componentId,scoringCriterionId]{componentId=9251, scoringCriterionId=2600}]
2015-07-03_14:27:15.715 INFO  o.h.e.i.DefaultLoadEventListener - HHH000327: Error performing load command : org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.categorization.TextScore.scoringCriterion
2015-07-03_14:27:15.717 INFO  o.h.e.i.DefaultLoadEventListener - HHH000327: Error performing load command : org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.categorization.TextScore.scoringCriterion
org.hibernate.PropertyAccessException: could not set a field value by reflection setter of model.categorization.TextScore.scoringCriterion
    at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:710)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:371)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4509)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:186)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1107)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:963)
    at org.hibernate.loader.Loader.doQuery(Loader.java:910)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2117)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3927)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:460)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:429)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:206)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:262)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1092)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1019)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:672)
    at org.hibernate.type.EntityType.resolve(EntityType.java:490)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1107)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:963)
    at org.hibernate.loader.Loader.doQuery(Loader.java:910)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2117)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3927)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:460)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:429)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:206)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:262)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1092)
    at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:175)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2476)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:988)
    at dao.inventory.ProgramDAO.getById(ProgramDAO.java:37)
    at service.inventory.InventoryService.getProgram(InventoryService.java:70)
    at service.categorization.ScoringCategorizationServiceIntegrationTests.aNewTextScoreForComponentShouldBeCreated(ScoringCategorizationServiceIntegrationTests.java:298)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.IllegalArgumentException: Can not set model.categorization.TextScoringCriterion field model.categorization.TextScore.scoringCriterion to model.categorization.NumericScoringCriterion
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139)
    ... 73 more

建议

Caused by: java.lang.IllegalArgumentException: Can not set model.categorization.TextScoringCriterion field model.categorization.TextScore.scoringCriterion to model.categorization.NumericScoringCriterion
听起来很奇怪,因为当我看到DB时,一切都是正确的。 接下来,我更改TextScore与评分标准的关系为ScoringCriterion类型而不是TextScoringCriterion

然后我得到了hibernate assign class NumericScoringCrietrion来反对DB中的对象:

2650;"ROI     ";"T";"Return on investment";"";;;"Pi�kny opis tego czym ten wska�nik jest";"Pytanie konkursowe numer jeden?"

根据&#34; T&#34;显而易见TextScoringCriterion在类型字段和&#34; Pytanie konkursowe numer jeden?&#34;在问题领域。

调试器:  http://i.stack.imgur.com/rst6Z.jpg

我已经与它斗争了一段时间,我不知道如何解决这个问题。

P.S。这是我的第一个问题,所以我无法发布图片。 ;)我将提供所有可能有用的信息。

0 个答案:

没有答案