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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:51:31+00:00 2026-05-18T01:51:31+00:00

I would like to make use @IndexColumn to set seq number of some data

  • 0

I would like to make use @IndexColumn to set seq number of some data the user enters. I am using Spring 2.5.6, JBoss 5.1 (JPA 1.0).

For my parent class

@Entity
@Table(name="material")
public class Material implements Serializable {
.
.
    /**
     * List of material attributes associated with the given material
     */
    @OneToMany(mappedBy = "material", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @IndexColumn(name="seq_number", base=0, nullable = false)
    private List<MaterialAttribute> materialAttributes;

    public void addMaterialAttribute(List<MaterialAttribute> attribs)
    {
        if(CollectionUtils.isNotEmpty(attribs))
        {
            for(MaterialAttribute attrib : attribs)
            {
                attrib.setMaterial(this);
            }

            this.setMaterialAttributes(attribs);
        }
    }

}

For my child class

@Entity
@Table(name="material_attribute")
public class MaterialAttribute implements Serializable
{

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "material_id", referencedColumnName = "id", updatable=false, nullable = true, unique = false)
    private Material material;

    @Column(name = "seq_number", insertable=false, updatable=false, nullable = false)
    private int seqNumber;
}

For the service class

public void save(MaterialCommand pCmd)
{
    Material material = new Material(pCmd.getName());

    //convert from command object to entity object
    List<MaterialAttribute> attribs = new ArrayList<MaterialAttribute>();

    if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
    {
        Iterator<MaterialAttributeCommand> iter = pCmd.getAttribs().iterator();
        while(iter.hasNext())
        {
            MaterialAttributeCommand attribCmd = (MaterialAttributeCommand) iter.next();

            MaterialAttribute attrib = new MaterialAttribute();
            attrib.setDisplayName(attribCmd.getDisplayName());
            attrib.setValidationType(attribCmd.getValidationType());

            attribs.add(attrib);
        }
    }

    material.addMaterialAttribute(attribs);

    this.getMaterialDAO().saveMaterial(material);
}

I am getting entries into the database but the seq_number is always zero, for every item in the collection.

I have to assume it is in the way that I am saving the data but I just do not see it.


I have been able to solve the issue doing the following (removed the mappedBy):

@Entity
@Table(name="material")
public class Material implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 5083931681636496023L;

    @Column(name="name", length=50, nullable=false)
    private String mName;

    /**
     * List of material attributes associated with the given material
     */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @IndexColumn(name="seq_number", base=0)
    @JoinColumn(name="material_id",nullable=false)
    private List<MaterialAttribute> materialAttributes;



@Entity
@Table(name="material_attribute")
public class MaterialAttribute implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = -196083650806575093L;

    /**
     * identifies the material that these attributes are associated with
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "material_id", insertable=false, updatable=false, nullable = true, unique = false)
    private Material material;

    @Column(name = "seq_number", insertable=false, updatable=false)
    private int seqNumber;
  • 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-18T01:51:31+00:00Added an answer on May 18, 2026 at 1:51 am

    Mapping a bidirectional indexed List with Hibernate is a bit tricky but is covered in the section 2.4.6.2.1. Bidirectional association with indexed collections of the documentation (bold is mine):

    2.4.6.2.1. Bidirectional association with indexed collections

    A bidirectional association where one
    end is an indexed collection (ie.
    represented as a @OrderColumn, or as
    a Map) requires special
    consideration. If a property on the
    associated class explicitly maps the
    indexed value, the use of mappedBy
    is permitted
    :

    @Entity
    public class Parent {
        @OneToMany(mappedBy="parent")
        @OrderColumn(name="order")
        private List<Child> children;
        ...
    }
    
    @Entity
    public class Child {
        ...
        //the index column is mapped as a property in the associated entity
        @Column(name="order")
        private int order;
    
        @ManyToOne
        @JoinColumn(name="parent_id", nullable=false)
        private Parent parent;
        ...
    }
    

    But, if there is no such property on
    the child class, we can’t think of
    the association as truly
    bidirectional
    (there is information
    available at one end of the
    association that is not available at
    the other end: the index). In this
    case, we can’t map the collection as
    mappedBy. Instead, we could use the
    following mapping:

    @Entity
    public class Parent {
        @OneToMany
        @OrderColumn(name="order")
        @JoinColumn(name="parent_id", nullable=false)
        private List<Child> children;
        ...
    }
    
    @Entity    
    public class Child {    
        ...
        @ManyToOne
        @JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
        private Parent parent;
        ...
    }
    

    Note that in this mapping, the
    collection-valued end of the
    association is responsible for
    updating the foreign key
    .

    Actually, the second mapping is precisely how to map a bidirectional one to many with the one-to-many side as the owning side. While this is possible, you need to be aware that this kind of mapping will produce under optimized SQL as stated in the section about 2.2.5.3.1.1. Bidirectional [One-to-many] relations:

    To map a bidirectional one to many,
    with the one-to-many side as the
    owning side, you have to remove the
    mappedBy element and set the many to
    one @JoinColumn as insertable and
    updatable to false. This solution is
    not optimized and will produce some
    additional UPDATE statements.

    To sum up, if mapping the index column as a property of the target entity is not a concern, this would be my recommendation (i.e. the first mapping).

    References

    • Hibernate Annotations 3.4 Reference Guide
      • 2.2.5.3.1.1. Bidirectional [One-to-many]
      • 2.4.6.2.1. Bidirectional association with indexed collections
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a project and I would like to make use of some
I'm using Forms authentication and I would like to make use of roles, can
I would like to make use of request scoped beans in my app. I
I would like to know how to make use of an Axiom database resource
I would like to use python to make system calls to programs and time
I would like to use the Roboto font in my Android application and make
I use ancestry to make a tree of goals. I would like to send
I would like to make use of embedded base64 images in ie7. Example: http://jsfiddle.net/mfPnK/
I would like to make use in a normal JDialog of the information icon
I have an MVC3 with nHibernate project, and I would like to make use

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.