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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:48:55+00:00 2026-05-15T14:48:55+00:00

I’m using Hibernate 3.5.2-FINAL with annotations to specify my persistence mappings. I’m struggling with

  • 0

I’m using Hibernate 3.5.2-FINAL with annotations to specify my persistence mappings. I’m struggling with modelling a relationship between an Application and a set of Platforms. Each application is available for a set of platforms.

From all the reading and searching I’ve done, I think I need to have the platform enum class be persisted as an Entity, and to have a join table to represent the many-to-many relationship. I want the relationship to be unidirectional at the object level, that is, I want to be able to get the list of platforms for a given application, but I don’t need to find out the list of applications for a given platform.

Here are my simplified model classes:

@Entity
@Table(name = "TBL_PLATFORM")
public enum Platform {
    Windows,
    Mac,
    Linux,
    Other;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id = null;

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

    private DevicePlatform() {
        this.name = toString();
    }

    // Setters and getters for id and name...
}

@Entity
@Table(name = "TBL_APP")
public class Application extends AbstractEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "NAME")
    protected String _name;

    @ManyToMany(cascade = javax.persistence.CascadeType.ALL)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @JoinTable(name = "TBL_APP_PLATFORM", 
              joinColumns = @JoinColumn(name = "APP_ID"),
              inverseJoinColumns = @JoinColumn(name = "PLATFORM_ID"))
    @ElementCollection(targetClass=Platform.class)
    protected Set<Platform> _platforms;

    // Setters and getters...
}

When I run the Hibernate hbm2ddl tool, I see the following (I’m using MySQL):

create table TBL_APP_PLATFORM (
    APP_ID bigint not null,
    PLATFORM_ID bigint not null,
    primary key (APP_ID, PLATFORM_ID)
);

The appropriate foreign keys are also created from this table to the application table and platform table. So far so good.

One problem I’m running into is when I try to persist an application object:

Application newApp = new Application();
newApp.setName("The Test Application");
Set<DevicePlatform> platforms = EnumSet.of(Platform.Windows, Platform.Linux);
newApp.setPlatforms(platforms);
applicationDao.addApplication(newApp);

What I would like to happen is for the appropriate rows in the Platform table to created, i.e. create a row for Windows and Linux, if they don’t already exist. Then, a row for the new application should be created, and then the mapping between the new application and the two platforms in the join table.

One issue I’m running into is getting the following runtime exception:

2010-06-30 13:18:09,382 6613126-0 ERROR FlushingEventListener Could not synchronize database state with session org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.example.model.Platform

Somehow, the platform set is not being persisted when I try to persist the application. The cascade annotations are supposed to take care of that, but I don’t know what’s wrong.

So my questions are:

  1. Is there a better way to model what I want to do, e.g. is using an Enum appropriate?
  2. If my model is alright, how do I properly persist all of the objects?

I’ve been struggling with this for hours, and I’ve tried to recreate all of the code above, but it might not be complete and/or accurate. I’m hoping someone will point out something obvious!

  • 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-15T14:48:55+00:00Added an answer on May 15, 2026 at 2:48 pm

    You should decide whether your Platform is an entity or not.

    If it’s an entity, it can’t be an enum, because list of possible platforms is stored in the database, not in the application. It should be a regular class with @Entity annotation and you will have a normal many-to-many relation.

    If it isn’t an entity, then you don’t need TBL_PLATFORM table, and you don’t have a many-to-many relation. In this case you can represent a set of Platforms either as an integer field with bit flags, or as a simple one-to-many relation. JPA 2.0 makes the latter case simple with @ElementCollection:

    @ElementCollection(targetClass = Platform.class) 
    @CollectionTable(name = "TBL_APP_PLATFORM",
        joinColumns = @JoinColumn(name = "APP_ID"))
    @Column(name = "PLATFORM_ID")
    protected Set<Platform> _platforms; 
    

    –

    create table TBL_APP_PLATFORM (   
        APP_ID bigint not null,   
        PLATFORM_ID bigint not null, -- the ordinal number of enum value   
        primary key (APP_ID, PLATFORM_ID)   
    );
    

    and enum Platform without annotations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.