Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1053271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:13:33+00:00 2026-05-16T17:13:33+00:00

I’m trying to map two objects to each other using a ManyToMany association, but

  • 0

I’m trying to map two objects to each other using a ManyToMany association, but for some reason when I use the mappedBy property, hibernate seems to be getting confused about exactly what I am mapping. The only odd thing about my mapping here is that the association is not done on a primary key field in one of the entries (the field is unique though).

The tables are:

Sequence (
  id NUMBER,
  reference VARCHAR,
)

Project (
  id NUMBER
)

Sequence_Project (
  proj_id number references Project(id),
  reference varchar references Sequence(reference)
)

The objects look like (annotations are on the getter, put them on fields to condense a bit):

class Sequence {
   @Id
   private int id;

   private String reference;

   @ManyToMany(mappedBy="sequences")
   private List<Project> projects;
}

And the owning side:

class Project {
    @Id
    private int id;

    @ManyToMany
    @JoinTable(name="sequence_project",
               joinColumns=@JoinColumn(name="id"),
               inverseJoinColumns=@JoinColumn(name="reference", 
                                     referencedColumnName="reference"))
    private List<Sequence> sequences;
}

This fails with a MappingException:

property-ref [_test_local_entities_Project_sequences] not found on entity [test.local.entities.Project]

It seems to weirdly prepend the fully qualified class name, divided by underscores. How can I avoid this from happening?

EDIT:
I played around with this a bit more. Changing the name of the mappedBy property throws a different exception, namely:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: test.local.entities.Project.sequences

So the annotation is processing correctly, but somehow the property reference isn’t correctly added to Hibernate’s internal configuration.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:13:33+00:00Added an answer on May 16, 2026 at 5:13 pm

    I have done the same scenario proposed by your question. And, as expected, i get the same exception. Just as complementary task, i have done the same scenario but with one-to-many many-to-one by using a non-primary key as joined column such as reference. I get now

    SecondaryTable JoinColumn cannot reference a non primary key

    Well, can it be a bug ??? Well, yes (and your workaround works fine (+1)). If you want to use a non-primary key as primary key, you must make sure it is unique. Maybe it explains why Hibernate does not allow to use non-primary key as primary key (Unaware users can get unexpected behaviors).

    If you want to use the same mapping, You can split your @ManyToMany relationship into @OneToMany-ManyToOne By using encapsulation, you do not need to worry about your joined class

    Project

    @Entity
    public class Project implements Serializable {
    
        @Id
        @GeneratedValue
        private Integer id;
    
        @OneToMany(mappedBy="project")
        private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();
    
        @Transient
        private List<Sequence> sequenceList = null;
    
        // getters and setters
    
        public void addSequence(Sequence sequence) {
            projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(id, sequence.getReference())));
        }
    
        public List<Sequence> getSequenceList() {
            if(sequenceList != null)
                return sequenceList;
    
            sequenceList = new ArrayList<Sequence>();
            for (ProjectSequence projectSequence : projectSequenceList)
                sequenceList.add(projectSequence.getSequence());
    
            return sequenceList;
        }
    
    }
    

    Sequence

    @Entity
    public class Sequence implements Serializable {
    
        @Id
        private Integer id;
        private String reference;
    
        @OneToMany(mappedBy="sequence")
        private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();
    
        @Transient
        private List<Project> projectList = null;
    
        // getters and setters
    
        public void addProject(Project project) {
            projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(project.getId(), reference)));
        }
    
        public List<Project> getProjectList() {
            if(projectList != null)
                return projectList;
    
            projectList = new ArrayList<Project>();
            for (ProjectSequence projectSequence : projectSequenceList)
                projectList.add(projectSequence.getProject());
    
            return projectList;
        }
    
    }
    

    ProjectSequence

    @Entity
    public class ProjectSequence {
    
        @EmbeddedId
        private ProjectSequenceId projectSequenceId;
    
        @ManyToOne
        @JoinColumn(name="ID", insertable=false, updatable=false)
        private Project project;
    
        @ManyToOne
        @JoinColumn(name="REFERENCE", referencedColumnName="REFERENCE", insertable=false, updatable=false)
        private Sequence sequence;
    
        public ProjectSequence() {}
        public ProjectSequence(ProjectSequenceId projectSequenceId) {
            this.projectSequenceId = projectSequenceId;
        }
    
        // getters and setters
    
        @Embeddable
        public static class ProjectSequenceId implements Serializable {
    
            @Column(name="ID", updatable=false)
            private Integer projectId;
    
            @Column(name="REFERENCE", updatable=false)
            private String reference;
    
            public ProjectSequenceId() {}
            public ProjectSequenceId(Integer projectId, String reference) {
                this.projectId = projectId;
                this.reference = reference;
            }
    
            @Override
            public boolean equals(Object o) {
                if (!(o instanceof ProjectSequenceId))
                    return false;
    
                final ProjectSequenceId other = (ProjectSequenceId) o;
                return new EqualsBuilder().append(getProjectId(), other.getProjectId())
                                          .append(getReference(), other.getReference())
                                          .isEquals();
            }
    
            @Override
            public int hashCode() {
                return new HashCodeBuilder().append(getProjectId())
                                            .append(getReference())
                                            .hashCode();
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.