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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:50:55+00:00 2026-05-26T10:50:55+00:00

I am trying to retrieve the generated ID of a newly created Entity within

  • 0

I am trying to retrieve the generated ID of a newly created Entity within a transaction, but when I try to read the ID value it is null. I assume this is because the transaction has not yet been committed and the Entity’s ID has yet to be created.

I am using Spring MVC and transactions (using @Transactional on my service), and using JPA for the data layer. I’m not an expert in transaction management, so I’m not even sure if this is possible. This is example code being executing in the presentation layer (Spring portlet MVC):

Long parentId = getParentId();
Folder parentFolder = linksService.getItem(parentId, Folder.class);
Folder newFolder;
newFolder = new Folder();
newFolder.setName("new folder");
newFolder.setParent(parentFolder);
parentFolder.addItem(newItem);
linksService.saveItem(parentFolder); // this calls entityManager.merge(parentFolder)

// this returns null
String itemId = newFolder.getItemId();

EDIT:

Here are the entities. I am using Oracle db.

@Entity
@Table(name = "LINK_ITEM")
@DiscriminatorColumn(name = "ITEM_TYPE")
public abstract class Item {

/**
 * The Id of this item
 */
@Id
@TableGenerator(name = "table_gen", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, 
    generator = "table_gen")
@Column(name = "ITEM_ID")
private Long itemId;

/**
 * The name of this item
 */
private String name;

/**
 * the parent item of this item
 */
@ManyToOne
@JoinColumn(name="PARENT_ID")
private Item parent;

/**
 * The user ID that owns this item
 */
private String owner;

/**
 * @return Returns the itemId.
 */
public Long getItemId() {
    return itemId;
}

/**
 * @param itemId
 *            The itemId to set.
 */
public void setItemId(Long itemId) {
    this.itemId = itemId;
}

/**
 * @return Returns the name.
 */
public String getName() {
    return name;
}

/**
 * @param name
 *            The name to set.
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return Returns the owner.
 */
public String getOwner() {
    return owner;
}

/**
 * @param owner
 *            The owner to set.
 */
public void setOwner(String owner) {
    this.owner = owner;
}

/**
 * @return Returns the parent.
 */
public Item getParent() {
    return parent;
}

/**
 * @param parent
 *            The parent to set.
 */
public void setParent(Item parent) {
    this.parent = parent;
}

/**
 * Returns the depth of this object in the folder tree. 0 is the top folder,
 * 1 is one level down, etc.
 * 
 * @return Returns the depth.
 */
@Transient
public long getDepth() {
    long i = 0;

    for (Item item = this; item.getParent() != null; item = item
            .getParent()) {
        ++i;
    }

    return i;
}

/**
 * Changes the parent folder of this item and updates links / children
 * appropriately.
 * 
 * @param parentFolder
 */
public void updateParent(Folder parentFolder) {
    removeFromParent();
    parentFolder.addItem(this);
}

/**
 * Removes this item from it's parent folder, if it has one.
 */
public void removeFromParent() {
    if (getParent() != null) {
        ((Folder) getParent()).removeItem(this);
    }
}

public void moveUp() {
    if (getParent() == null) {
        return;
    }

    Folder parent = (Folder) getParent();
    List<Item> siblings = parent.getChildren();

    int index = siblings.indexOf(this);
    if (index > 0) {
        Item previousItem = siblings.get(index - 1);
        siblings.set(index, previousItem);
        siblings.set(index - 1, this);
    }
}

public void moveDown() {
    if (getParent() == null) {
        return;
    }

    Folder parent = (Folder) getParent();
    List<Item> siblings = parent.getChildren();

    int index = siblings.indexOf(this);
    int numItems = siblings.size();

    if ((numItems > 1) && (index < (numItems - 1))) {
        Item nextItem = (Item) siblings.get(index + 1);
        siblings.set(index, nextItem);
        siblings.set(index + 1, this);
    }
}

/**
 * Returns the String representation of this Item.
 */
@Override
public String toString() {
    return "itemId=" + this.getItemId() + "; name=" + this.getName()
            + "; owner=" + this.getOwner();
}
}



@Entity
@DiscriminatorValue("F")
public class Folder extends Item {

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="PARENT_ID", referencedColumnName="ITEM_ID")
private List<Item> children = new ArrayList<Item>();

@Transient
private String path;

@Transient
private boolean open;

@Transient
private Collection<Link> orderedLinks;

/**
 * @return Returns the children.
 */
public List<Item> getChildren() {
    return children;
}

/**
 * @param children
 *            The children to set.
 */
public void setChildren(List<Item> children) {
    this.children = children;
}

/**
 * Changes the parent folder of this item and updates links / children
 * appropriately.
 * 
 * @param parentFolder
 */
public void updateParent(Folder parentFolder) {
    super.updateParent(parentFolder);

    // update the path since the parent folder has changed
    updatePath();
}

/**
 * @param newItem
 */
public void addItem(Item newItem) {
    newItem.setParent(this);
    getChildren().add(newItem);
}

/**
 * @param item
 */
public void removeItem(Item item) {
    getChildren().remove(item);
    item.setParent(null);
}

/**
 * 
 * @param items
 */
public void addItems(List<? extends Item> items) {
    for (Item item : items)
        addItem(item);
}

/**
 * 
 * @param items
 */
public void removeItems(List<? extends Item> items) {
    for (Item item : items)
        removeItem(item);
}

/**
 * Returns a list of Folder objects that are the subfolders of this folder.
 * This folder is also included at the top of the list.
 * 
 * @return
 * @throws ServiceException
 */
@Transient
public List<Folder> getFolderList() {
    List<Folder> folderList = new ArrayList<Folder>();
    buildFolderList(folderList, null);

    return folderList;
}

/**
 * Returns a list of Folder objects that are the subfolders of this folder.
 * This folder is also included at the top of the list. This method will
 * exclude the "excludeFolder" and it's subfolders from the list.
 * 
 * @param excludeFolder
 * @return
 */
@Transient
public List<Folder> getFolderList(Folder excludeFolder) {
    List<Folder> folderList = new ArrayList<Folder>();
    buildFolderList(folderList, excludeFolder);

    return folderList;
}

/**
 * Returns a recursive list of the parent folder of this folder. Includes
 * this folder in the list.
 * 
 * @return
 */
@Transient
public List<Folder> getParentList() {
    List<Folder> parentList = new ArrayList<Folder>();
    Folder currentFolder = this;
    parentList.add(currentFolder);

    while (currentFolder.getParent() != null) {
        currentFolder = (Folder) currentFolder.getParent();
        parentList.add(currentFolder);
    }

    // reverse the ordering
    Collections.reverse(parentList);

    return parentList;
}

/**
 * Private method called recursively to build a list of Folder's and
 * subfolders of the parentFolder.
 * 
 * @param folderList
 * @param parentFolder
 * @param excludeFolder
 */
private void buildFolderList(List<Folder> folderList, Folder excludeFolder) {
    // Don't add the exclude folder to the list
    if (excludeFolder != null && this.equals(excludeFolder)) {
        return;
    }

    folderList.add(this);

    if (!isFolderEmpty()) {
        for (Item item : getChildren()) {

            if (item instanceof Folder) {
                ((Folder) item).buildFolderList(folderList, excludeFolder);
            }
        }
    }
}

/**
 * @return Returns the folderEmpty.
 */
@Transient
public boolean isFolderEmpty() {
    return children == null || children.isEmpty() || children.size() == 0;
}

/**
 * 
 */
private void updatePath() {
    StringBuffer strBuffer = new StringBuffer("");
    strBuffer.append(getName());
    Item parent = getParent();

    while (parent != null) {
        strBuffer.insert(0, parent.getName() + " > ");
        parent = parent.getParent();
    }

    this.path = strBuffer.toString();
}

/**
 * @return Returns the path of this folder.
 */
public String getPath() {

    if (this.path == null || this.path.length() == 0)
        updatePath();

    return this.path;
}

/**
 * @param path
 *            The path to set.
 */
protected void setPath(String path) {
    this.path = path;
}

public Item find(Long itemId) {
    if (itemId.equals(getItemId()))
        return this;

    Item item = null;
    List<Item> children = getChildren();

    for (Item currentItem : children) {
        if (currentItem.getItemId().equals(itemId)) {
            item = currentItem;
            break;
        } else if (currentItem instanceof Folder) {
            item = ((Folder) currentItem).find(itemId);

            if (item != null)
                break;
        }
    }
    return item;
}

/**
 * Returns the String representation of this Folder.
 */
@Override
public String toString() {
    return super.toString() + "; path=" + this.getPath();
}

/**
 * 
 * @return a list of Link objects that this Folder holds.
 */
@Transient
public List<Link> getLinks() {
    List<Item> children = getChildren();
    List<Link> links = new ArrayList<Link>(children.size()
            - (children.size() / 2));

    for (Item item : children) {

        if (item instanceof Link) {
            links.add((Link) item);
        }
    }

    return links;
}

/**
 * Returns the child Folders of this Folder and their child Folders, etc.
 * 
 * @return
 */
@Transient
public List<Folder> getChildFolders() {
    List<Folder> folderList = new ArrayList<Folder>();
    buildFolderList(folderList, null);
    folderList.remove(this);

    return folderList;
}

public boolean isOpen() {
    return open;
}

@Transient
public boolean isClosed() {
    return !open;
}

public void setOpen(boolean open) {
    this.open = open;
}

public Collection<Link> getOrderedLinks() {
    return orderedLinks;
}

public void setOrderedLinks(Collection<Link> orderedLinks) {
    this.orderedLinks = orderedLinks;
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj == null || (obj.getClass() != this.getClass())) {
        return false;
    }

    Folder folder = (Folder) obj;
    return this.getItemId() == folder.getItemId();
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    int var = (int) (this.getItemId().longValue() ^ (this.getItemId()
            .longValue() >>> 32));
    int hash = 7;
    hash = 31 * hash + var;

    return hash;
}
}



@Entity
@DiscriminatorValue("L")
public class Link extends Item {

private String url;

public Link() {
}

public Link(String url) {
    this.url = url;
}

/**
 * @return Returns the url.
 */
public String getUrl() {
    return url;
}

/**
 * @param url
 *            The url to set.
 */
public void setUrl(String url) {
    if (url != null && url.indexOf(":/") == -1)
        url = "http://" + url;

    this.url = url;
}
}

The Controller is calling the DAO, which calls entityManager.merge() (and I tried including entityManger.flush()). I also use OpenEntityInManagerInterceptor.

  • 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-26T10:50:56+00:00Added an answer on May 26, 2026 at 10:50 am

    Since you are using TABLE sequencing the id will be assign on your persist() call. Are you detaching or serializing the objects? You may need to return the id assigned in persist to your client.

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

Sidebar

Related Questions

I'm trying to retrieve this page using Apache HttpClient: http://quick-dish.tablespoon.com/ Unfortunately, when I try
Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I am trying to retrieve the following json generated in my php script uing
I am trying to retrieve content from a dynamic XML generated in Perl proxy.
I'm trying to implement an autocomplete field. I want this autocomplete to retrieve the
I've been trying to figure this out all night, but I guess my knowledge
I am trying this in an XQuery (assume that doc('input:instance') does indeed return a
I'm trying to retrieve a single entity by its ID in the DomainService .
I am trying to use SharePoint Web service to retrieve list changes but there
I am trying retrieve user name from the code through graph api, the below

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.