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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:20:18+00:00 2026-06-15T12:20:18+00:00

I have a class which im trying to implement, (similar to the example in

  • 0

I have a class which im trying to implement, (similar to the example in ‘Java Persistence with Hibernate’ 6.3.3)

public class Property
{
    ...

    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionId(
        columns=@Column(name="Property_image_id"),
        type=@Type(type="long"),
        generator="native"
    )
    private Collection<FileAttachment> images = new ArrayList<FileAttachment>();

    ...
}

The unit test fails throwing the following exception:

java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2 
    cannot be cast to java.lang.Long

Im not too sure about the best value for ‘generator’, and I assume this will affect the outcome.

Also when i get this working, can the FileAttachment object access the Property_image_id? And how do you assign it to a property, since its defined in the Property class?

What I’d like is for the Property_images table to have a composite key [Property_id-Image_index], where Image_index starts at 1 for each new Property_id but I have no idea how to implement this using @ElementCollection and @CollectionId with a generator. Maybe i have to have FileAttachment as an @Entity instead of @Embeddable, but id rather not as its only used inside the Property class.

Cheers!
NFV

  • 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-15T12:20:19+00:00Added an answer on June 15, 2026 at 12:20 pm

    From Java Persistence Wikibook:

    The JPA 2.0 specification does not provide a way to define the Id in
    the Embeddable. However, to delete or update a element of the
    ElementCollection mapping, some unique key is normally required.
    Otherwise, on every update the JPA provider would need to delete
    everything from the CollectionTable for the Entity, and then insert
    the values back. So, the JPA provider will most likely assume that the
    combination of all of the fields in the Embeddable are unique, in
    combination with the foreign key (JoinColumn(s)). This however could
    be inefficient, or just not feasible if the Embeddable is big, or
    complex. Some JPA providers may allow the Id to be specified in the
    Embeddable, to resolve this issue. Note in this case the Id only needs
    to be unique for the collection, not the table, as the foreign key is
    included. Some may also allow the unique option on the CollectionTable
    to be used for this. Otherwise, if your Embeddable is complex, you may
    consider making it an Entity and use a OneToMany instead.

    I think that your IMAGE_INDEX requirement can be tackled with OrderColumns:

    @ElementCollection(fetch=FetchType.EAGER)
    @OrderColumn(name = "IMAGE_INDEX")
    private List<FileAttachment> images;
    

    This post may be useful.

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

Sidebar

Related Questions

I'm trying to implement a Hibernate persistence layer in a Java application and I'm
I'm trying to implement the following: I have an Importer class which registers so
I am trying to create downloading progress. I have my class which extends AsyncTask:
I have the following javascript class which I am trying to pass to an
I'm trying to hide window after its startup. I have own window-class which is
I'm trying to validate an ID. I have this class called ManejadorTickets in which
I have class which have one public method Start , one private method and
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
I have a class which I am trying to use to test HashMap and
I'm trying to implement delegation for a class which should call it's delegate (if

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.