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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:57:59+00:00 2026-06-11T11:57:59+00:00

Having a problem getting some data persisted with hibernate I declare two independent classes,

  • 0

Having a problem getting some data persisted with hibernate

I declare two independent classes, License and MacAddress. MacAddress can exist in its own right, but it can also be linked to to a license, when hibernate generates the underlying tables it creates a License, MacAddress and License_MacAddress table.

License

@Entity
public class License
{
    @Id
    @GeneratedValue
    private Integer      id;

    @Column(nullable = false)
    private String      license;

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private
    List<MacAddress> macAddresses;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getLicense()
    {
        return license;
    }

    public void setLicense(String license)
    {
        this.license = license;
    }

    public List<MacAddress> getMacAddresses()
    {
        return macAddresses;
    }

    public void setMacAddresses(List<MacAddress> macAddresses)
    {
        this.macAddresses = macAddresses;
    }
}

MacAddress

@Entity
public class MacAddress
{
    @Id
    @GeneratedValue
    private Integer      id;

    @Column(nullable = false)
    private String      macAddress;


    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getMacAddress()
    {
        return macAddress;
    }

    public void setMacAddress(String macAddress)
    {
        this.macAddress = macAddress;
    }
}

What Im trying to do is the following.

At some point a new license is created and a row is stored in the License table (this works correctly)

Then at a later time the license, and macaddress of the user is received. The license is retrieved from the database , we check to see if the provided macAddress is already associated with the license (multiple macaddresses can be associated with the license) and if not we then want to store the macaddress and the link nbetween license and macaddress.

But despite setting CascadeType.ALL on the @OneToMany annotation of License, no Mac Address information get’s stored. I can manually save the MacAddress by saving that object seperately but still the License_MacAddress table remains empty.

Code

session = Db.getSession();
License license = getLicense(session, licenseStr);
List<MacAddress> macAddresses = license.getMacAddresses();
for(MacAddress mac:macAddresses)
{
    if(mac.getMacAddress().equals(macAddress))
    {
        return;
    }
}

MacAddress mac = new MacAddress();
mac.setMacAddress(macAddress);
session.save(mac);
license.getMacAddresses().add(mac);
session.saveOrUpdate(license);
return;

Why can I not get the relationship between the two persisted ?

  • 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-11T11:58:01+00:00Added an answer on June 11, 2026 at 11:58 am

    Hibernate follows Unit of Work pattern when saving changes to the database.

    You open a unit of work, apply changes to objects associated with it and Hibernate automatically saves your changes when you close the unit of work.

    In practical usage of Hibernate API the concept of Unit of Work usually matches Hibernate transaction, therefore you need a transaction in this case:

    session = Db.getSession(); 
    Transaction tx = null; 
    try { 
        tx = session.beginTransaction(); 
        License license = getLicense(session, licenseStr); 
        List<MacAddress> macAddresses = license.getMacAddresses(); 
        for(MacAddress mac:macAddresses) 
        { 
            if(mac.getMacAddress().equals(macAddress)) 
            { 
                return; 
            } 
        } 
    
        MacAddress mac = new MacAddress(); 
        mac.setMacAddress(macAddress); 
        license.getMacAddresses().add(mac);     
        tx.commit(); 
    } catch (RuntimeException e) { 
        if (tx != null) tx.rollback(); 
        throw e; // or display error message 
    } finally { 
        session.close(); 
    }
    

    See also:

    • 13.1.1. Unit of work
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem getting some data since I'm just starting to use LINQToEntities,
Currently i am having some problems with getting some data out of a DataTable
I'm having a problem with getting json data. I created 2 examples. One works,
I'm having some problem with getting Python to handle my unicode text correctly. I've
I am having some problem in my data base connection when I am trying
I'm having some issue getting what seems like a fairly straight-forward problem solved in
I'm having some problems getting ncurses' getch() to block. Default operation seems to be
Im having some problems getting the Sticky Footer to work on my site. If
I am having some problems getting my JS/JQ to fire in HTML5 page. Basically
I am having some problems getting a larger application with many table relationships working

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.