JPA: bestehen nicht einfügen in die join-Tabelle

Alle,

Ich bin mit JPA für diese Anwendung und Annotationen für das Mapping von Entitäten. Ich habe eine Entität namens UserStory und ein anderer namens Revision. Es ist ein OneToMany für UserStory zur 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;

hier nach der Überarbeitung der Organisation:

@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;

Wenn ich eine userStory; ich füge eine revision es,

aber die join-Tabelle wird nicht aufgefüllt, es sei denn, ich anhalten, Geschichte, die erste,
dann Version hinzufügen, und führen Sie Sie.

hier ist der code für speichern einer 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 ich nenne die persist-Methode:

@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;
  }

hier ist die SQL-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=?

Kann ich von hier aus sehen, es speichert die user story und revision, aber dann versucht, führen Sie einen join, um zu sehen, ob die Beziehung existiert, bevor ich ein insert in der join-Tabelle. Die es natürlich nicht finden, weil ich am erstellen dieses Objekts.

Wie es die join-Tabelle aufgefüllt, in diesem Fall?

InformationsquelleAutor Anadi Misra | 2011-10-19
Schreibe einen Kommentar