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

  • SEARCH
  • Home
  • 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 9015003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:38:38+00:00 2026-06-16T03:38:38+00:00

I am trying to use hibernate annotations to insert data to a MySQL database

  • 0

I am trying to use hibernate annotations to insert data to a MySQL database table which doesn’t have a primary key defined.

However the fact is 2 fields of that table together are unique in the table.how can i achieve the same using hibernate annotation?.

here is my code..

 @Entity
 @Table(name = "RolesMenuItems")
    public class RolesMenuItems {

       @Column(name = "RoleID")
       private String roleID;

       @Column(name = "MenuItemID")
       private String menuItemID;
  /*setter getter methods */
 }
  • 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-06-16T03:38:40+00:00Added an answer on June 16, 2026 at 3:38 am

    You can use @Embeddeble and @EmbeddedId to create a composite key and map it with your Entity. For example:

    @Embeddable
    public class RolesMenu {
        @Column(name = "RoleID")
        private String roleID;
    
        @Column(name = "MenuItemID")
        private String menuItemID;
    
        //getter, setter methods
    }
    
     @Entity
     @Table(name = "RolesMenuItems")
     public class RolesMenuItems {
    
         @EmbeddedId
         private RolesMenu roleMenu;
    
      /*setter getter methods */
     }
    

    Then use RolesMenuItems in your Java code to persist entities in usual way.

    Reference: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e535

    Edit:
    To persist the entity:

    RolesMenu roleMenu = new RolesMenu();
    roleMenu.setRoleID(...);
    roleMenu.setMenuItemID(...);
    
    RolesMenuItems roleItem = new RolesMenuItems();
    roleItem.setRoleMenu( roleMenu );
    
    em.persist(roleItem);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use composition in hibernate with annotations. I have: @Entity @Table(name =
I am trying to use Hibernate validator to validate POJO before insert into database.
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
My application is trying to use Hibernate annotations. I've gotten the 3.5.0 version of
I am trying to use hibernate with annotations. I annotated my class with @Entity
I have been trying to use Hibernate for a while. I like hibernate that
I'm trying to use Hibernate 4 with annotations only, and a hibernate.cfg.xml file. I've
I was trying some hands on with Hibernate annotations. When I tried to use
I'm trying to use Hibernate to retrieve approximately 100 million rows from a table.
I'm trying to configure spring data auditing. I use Hibernate 4.1.6, hibernate-jpa 2.0, spring-data-jpa

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.