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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:17:50+00:00 2026-05-28T08:17:50+00:00

All, I am using JPA for this application and annotations for Mapping entities. I

  • 0

All,

I am using JPA for this application and annotations for Mapping entities. I have an entity called UserStory and another one called Revision. There is a OneToMany for UserStory to Revision.

@Entity
@Table(name = "user_story")
@NamedNativeQueries({
    @NamedNativeQuery(name = "storyBacklog", query = "SELECT userstory.rank AS rank, userstory.description AS description, userstory.estimate AS estimate, userstory.name AS name, "
        + "userstory.id AS id, userstory.status AS status FROM user_story userstory ORDER BY userstory.rank ASC", resultClass = UserStory.class),
    @NamedNativeQuery(name = "getCos", query = "SELECT conditions.cos As cos FROM story_cos conditions WHERE conditions.story_id=?1", resultSetMapping = "cosMapping") })
@SqlResultSetMappings({ @SqlResultSetMapping(name = "cosMapping", columns = @ColumnResult(name = "cos")) })
public class UserStory implements Serializable {

  private static final long serialVersionUID = 248298400283358441L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  ...
  @OneToMany(cascade = CascadeType.ALL)
  @JoinTable(name = "story_revisions", joinColumns = @JoinColumn(name = "story_id"), inverseJoinColumns = @JoinColumn(name = "revision_id"))
  private Set<Revision> revisions;

here’s Revision entity:

@Entity
@Table(name = "revision")
public class Revision implements Serializable {

  private static final long serialVersionUID = -1823230375873326645L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column(nullable = false)
  private String description;

  @Column(name = "date_created", nullable = false)
  @Temporal(TemporalType.TIMESTAMP)
  private Date creationDate;

When I create a userStory; I add a revision on to it,

but the join table is not populated unless, I persist story first,
then add revision and merge it.

here’s the code for saving a UserStory:

  public UserStory saveUserStory(UserStory userStory) {
    Revision revision = new Revision();
    revision.setCreationDate(new Timestamp(System.currentTimeMillis()));
    revision.setDescription("User story created");
    Set<Revision> revisions = new HashSet<Revision>();
    revisions.add(revision);
    userStory.setRevisions(revisions);
    return storyDao.create(userStory);
  }

in StoryDao I call the persist method:

@Transactional(readOnly = false)
  public UserStory create(UserStory userStory) {
    if (userStory.getRank() == null) {
      Integer highestRank = 0;
      highestRank = (Integer) entityManager.createNativeQuery("select max(rank) from user_story")
          .getSingleResult();
      if (highestRank != null)
        highestRank += 1;
      else
        highestRank = new Integer(1);
      userStory.setRank(highestRank);
    }
    entityManager.persist(userStory);
    LOGGER.debug("Added User Story with id " + userStory.getId());
    entityManager.detach(userStory);
    return userStory;
  }

here’s the SQL from LOGS

Hibernate: 
    insert 
    into
        user_story
        (description, estimate, name, rank, status) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        revision
        (date_created, description) 
    values
        (?, ?)
Hibernate: 
    select
        revision0_.id as id5_0_,
        revision0_.date_created as date2_5_0_,
        revision0_.description as descript3_5_0_ 
    from
        revision revision0_ 
    where
        revision0_.id=?
Hibernate: 
    select
        userstory0_.id as id3_1_,
        userstory0_.description as descript2_3_1_,
        userstory0_.estimate as estimate3_1_,
        userstory0_.name as name3_1_,
        userstory0_.rank as rank3_1_,
        userstory0_.status as status3_1_,
        revisions1_.story_id as story1_3_3_,
        revision2_.id as revision2_3_,
        revision2_.id as id5_0_,
        revision2_.date_created as date2_5_0_,
        revision2_.description as descript3_5_0_ 
    from
        user_story userstory0_ 
    left outer join
        story_revisions revisions1_ 
            on userstory0_.id=revisions1_.story_id 
    left outer join
        revision revision2_ 
            on revisions1_.revision_id=revision2_.id 
    where
        userstory0_.id=?

I can see from here it saves the user story and revision, but then tries to run a join to see if the relation exists before doing an insert into the join table. Which of course it will not find because I am creating this object.

How do it get the join table populated in this case?

  • 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-28T08:17:51+00:00Added an answer on May 28, 2026 at 8:17 am

    Works now. Here’s the updated code

    revisions.add(revision);
    userStory = storyDao.create(userStory);
    userStory.setRevisions(revisions);
    return storyDao.update(userStory);
    

    I am still not sure why this is required; the two step method where I persist an object then update it.

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

Sidebar

Related Questions

I have an application using hibernate 3.1 and JPA annotations. It has a few
I am trying to set up Entity Inheritance using JPA annotations and abstract classes.
Hi guys I am migrating an application using hibernate from xml to JPA annotations.
I have been building a JSF application using JPA to access the DB. There
I have a fairly standard Java EE6 web application using JPA 2 with dependency
I'm developing an example web-application, using JPA 2.0 entities, Hibernate 3.6.2 and Spring 3.
We have a large number of programmers on different platforms all using CVS. We
I'm using all of the below to take a field called 'code' from my
i have several controllers which will all be using common functionality. So i have
I have about 10 different entities in my J2EE application that right now share

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.