Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate referencedColumnNames not mapped to a single property

i am getting the below exception on my spring 3 web application using Hibernate 4:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in URL [file:war/WEB-INF/datasource-config.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: referencedColumnNames(DISCUSSION_ID) of com.jr.freedom.discusion.Message.discussion referencing com.jr.freedom.discusion.Discussion not mapped to a single property
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
    at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.AnnotationException: referencedColumnNames(DISCUSSION_ID) of com.jr.freedom.discusion.Message.discussion referencing com.jr.freedom.discusion.Discussion not mapped to a single property
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:205)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1437)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 29 more

Below is the Message POJO that is using the discission Pojo:

@Entity
@Table(name = "MESSAGE")
public class Message {

    @Id
    @Column(name = "message_id")
    private String id;

    @Column(name = "message_text", nullable = false)
    private String messageText;

    /**
     * Foreign key for user table
     */
    @Id
    @OneToOne
    @JoinColumn(name = "from_user", nullable = false)
    private User fromUser;

    /**
     * Foreign key for discussion table
     */
    @Id
    @ManyToOne
    @JoinColumn(name = "discussion",referencedColumnName="DISCUSSION_ID", nullable = false)
    private Discussion discussion;

    public Message() {
        id = UUID.randomUUID().toString();
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public User getFromUser() {
        return fromUser;
    }

    public void setFromUser(User fromUser) {
        this.fromUser = fromUser;
    }

    public Discussion getDiscussionId() {
        return discussion;
    }

    public void setDiscussionId(Discussion discussion) {
        this.discussion = discussion;
    }

    public String getId() {
        return id;
    }

}

Here is the Discussion POJO:

@Entity
@Table(name = "DISCUSSION")
public class Discussion {

    @Id
    @Column(name = "DISCUSSION_ID")
    private String id;

        @Column(name = "TITLE", nullable = false)
    private String title;

    @Column(name = "CATAGORY", nullable = false)
    private String catagory;

    @Id
    @OneToOne
    @JoinColumn(name = "discussionOwner", nullable = false)
    private User discussionOwner;

    public Discussion() {
        id = UUID.randomUUID().toString();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCatagory() {
        return catagory;
    }

    public void setCatagory(String catagory) {
        this.catagory = catagory;
    }

    public User getDiscussionOwner() {
        return discussionOwner;
    }

    public void setDiscussionOwner(User discussionOwner) {
        this.discussionOwner = discussionOwner;
    }

    public String getId() {
        return id;
    }

}

What am i missing? the relationship between these two entities is that a discussion entity can contain many messages and a message can only belong to one discussion

Thanks in advance

like image 928
Jonathan Avatar asked Jun 26 '12 11:06

Jonathan


1 Answers

For those that are interested in doing a composite key join, try:

@JoinColumns({    
  @JoinColumn(name = "x", referencedColumnName = "x"),
  @JoinColumn(name = "y", referencedColumnName = "y"),
  @JoinColumn(name = "z", referencedColumnName = "z")
})

Essentially, if you have 3 columns marked as @ID, JPA assumes that you need all 3 columns for a unique identification. This means that on the foreign key side, you also need 3 columns to make a proper foreign key reference.

like image 149
Jeff Wang Avatar answered Oct 15 '22 07:10

Jeff Wang