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 1073865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:02:45+00:00 2026-05-16T21:02:45+00:00

I’m pretty lost with mapping the following structure with JPA annotations. +===========+ +====================+ |

  • 0

I’m pretty lost with mapping the following structure with JPA annotations.

+===========+             +====================+
| Offer     |             | Text               |
+-----------+ 1      0..* +--------------------+
| id (pk)   |-------------| textkey (pk)       |
| namekey   |             | languagecode (pk)  |
| ...       |             | text               |
+===========+             | ...                |
                          +====================+

So, each Offer has a name which is i18n-aware. As I have the same cases over and over in the application (Offer also has a i18n comment, Article has a i18n name, etc.) I want to have a Text entity with a composite primary key. For each key there are as many records as there are supported languages. Text samples:

+=====================================+
| textkey    | languagecode | text    |
+=====================================+
| offer5Name | en           | foo     |
| offer5Name | fr           | bar     |
| offer6Name | en           | hello   |
...

The Offer entity would store Text#textkey in its namekey column.

On the Java side I’d like Offer to have a Set of names or even better a Map of names so I could have a method like Text getName(String language) instead of Set<Text> getNames().

What I already have is Text and its composite primary key TextPK:

@Entity
public class Text {

  @EmbeddedId
  private TextPK primaryKey;

  @Column(name = "text")
  private String text;

PK

@Embeddable
public class TextPK implements Serializable {

  @Column(name = "textkey")
  private Long key;

  @Column(name = "languagecode")
  @Enumerated(EnumType.STRING)
  private LanguageCode languageCode;

Question: how do I annotate the ‘names’ member variable in the Offer class to get what I need?

  • 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-16T21:02:45+00:00Added an answer on May 16, 2026 at 9:02 pm

    Ok, again I’m answering my own question…

    JPA 1.0 does not support unidirectional OneToMany (http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_JPA_2.0_unidirectional_OneToMany_relationship_database) and that’s kind of what I would have ended up with.

    What worked best for my case was to create an intermediary TextCollection entity. Each domain entity (such as Offer) has a OneToOne relationship to a TextCollection for each of its text attributes (name, description, etc.). The collection entity itself has nothing but an id and bidirectional OneToMany relationship to Text.

    @Entity
    @Table(name = "textcollection")
    public class TextCollection {
    
      @Id
      @Column(name = "textkey")
      private String key;
    
      @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "textCollection")
      private final Set<Text> texts = new HashSet<Text>();
    
    
    @Entity
    @Table(name = "text")
    public class Text {
    
      @EmbeddedId
      private TextPK primaryKey;
    
      @Column(name = "text", nullable = false)
      private String text;
    
      @ManyToOne
      @JoinColumn(name = "textkey", insertable = false, updatable = false)
      private TextCollection textCollection;
    
    @Entity
    @Table(name = "offer")
    public class Offer {
    
      @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinColumn(name = "namekey", nullable = false, insertable = true)
      private TextCollection name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.