简体   繁体   中英

Hibernate not updating ManyToMany associations during session

I am working on a project using Hibernate 4.3.4 to access a Postgres DB. We have two entities which are linked via a ManyToMany Association.

The code and the associations currently work, in that adding an EntityB to EntityA's collection will automatically add the EntityA to the EntityB's collection once the Session is committed. However, the issue I'm having is that when I try to work on the EntityB's EntityAs, which should include the EntityA I just created, EntityA is not in that collection (It is empty). Example code is here:

@Entity
@Table(name = "entity_a")
public class EntityA {
    private Set<EntityB> entityBs = new HashSet<EntityB>(0);

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "entitya_entityb",
            joinColumns = { @JoinColumn(name = "entitya_id") },
            inverseJoinColumns = { @JoinColumn(name = "entityb_id") })
    public Set<EntityB> getEntityBs()
    {
        return entityBs;
    }

    public void setEntityBs(Set<EntityB> entityBs)
    {
        this.entityBs = entityBs;
    }
}

@Entity
@Table(name = "entity_b")
public class EntityB {
    private Set<EntityA> entityAs = new HashSet<EntityA>(0);

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "entitya_entityb",
            joinColumns = { @JoinColumn(name = "entityb_id") },
            inverseJoinColumns = { @JoinColumn(name = "entitya_id") })
    public Set<EntityA> getEntityAs()
    {
        return entityAs;
    }

    public void setEntityAs(Set<EntityA> entityAs)
    {
        this.entityAs = entityAs;
    }
}

/**
 * HTTP REST Resource to create Entities and persist them. We do some basic logic when we create them to show the problem.
 */
@Path("/battleRhythm")
@Singleton
public class HttpResource
{
    @POST
    @Consumes("application/json")
    public void createEntityA() {
        Session hibernateSession = SessionFactory.getCurrentSession(); // SessionFactory specifics not included
        hibernateSession.getTransaction().begin();

        // Add an EntityB to the new EntityA
        EntityA entityA0 = new EntityA();
        EntityB entityB0 = new EntityB0();
        entityA.getEntityBs().add(entityB0);

        // Persist the new EntityA
        EntityADao.getInstance().save(entityA0);

        // Try to get this EntityA from EntityB
        Set<EntityA> associatedEntityAs = entityB0.getEntityAs(); // Doesn't contain any EntityAs!

        hibernateSession.getTransaction().commit();
    }
}

Here's the question:

Can I make Hibernate automatically add the EntityA0 to EntityB0's collection when I save EntityA0, without committing the transaction? Is this possible? How?

Caveat : The example above does not fully reflect this, but we perform similar operations on both Entities, so having an "owner" in the traditional Hibernate sense (using the mappedBy = "" Attribute configuration) is not an ideal option. I don't want to try to convince everyone to only ever use EntityB.getEntityAs().add(EntityB0) in CreateEntityA(). It's too confusing.

You don't have the choice. There MUST be an owner side, and there MUST be an inverse side. And it's YOUR responsibility to maintain both sides of the association: don't expect to have B inside A's collection of Bs when you only add A to B (and vice-versa)

Now, nothing forbids you to have a methods addB(B b) inside A that adds b to A's collection of Bs, and which adds this to B's collection of As. And you can of course also have a method addA(A a) in B that does the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM