0

I have a spring mvc application running on glassfish. I don't know the source of my problem so ill elaborate as much as possible. I have 2 Classes
A User Class

@Entity
@Table(name="Users")
public class User extends BaseEntity<User> {
    // other fields

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
    @OrderBy(value = "id DESC")
    private Set<OtherClass> objs;

    // getters and setters         
}

and an OtherClass:

@Entity
@Table(name="OtherObjects")
public class OtheClass extends BaseEntity<OtheClass> {
    // other fields

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "User", nullable = false)
    private User user;

    // getters and setters
}

i retrieve a user object when the application starts and put it in the session object. while runtime, the user can add to the DB a "OtherClass" object. the otherClass object is saved correctly in the DB (with a relation to the correct user row in the Users table). the thing is that after the user added the otherClass object at runtime, the Set of other objects doesn't update. the weirder thing is that even if i retrieve the user object from the DB again, i don't get the updated Set of OtherClass. This is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="myPU" transaction-type="JTA">

        <jta-data-source>jdbc/MyDataSource</jta-data-source>

        <class>org.company.entities.OtherClass</class>
        <class>org.company.entities.User</class>

        <exclude-unlisted-classes />

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.current_session_context_class" value="thread" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>

    </persistence-unit>
</persistence>

I recently changed my transaction manager from the jpa transaction manager of spring to JTA transaction manager. (i suppose it has something to do with it). Any ideas? if further code or configuration is required, please tell and i'll post an update.

Marc
  • 16,170
  • 20
  • 76
  • 119
Mr T.
  • 4,278
  • 9
  • 44
  • 61
  • You might need to evict the object before re-reading it from the database. Otherwise it can use a cached copy. – JustinKSU Nov 12 '12 at 18:52
  • i thought it might be something like this so i tried to find a "no cache" configuration via the persistence.xml. know of any? – Mr T. Nov 12 '12 at 18:54

1 Answers1

0

JPA doesn't maintain bidirectional associations automatically, it's up to the developer to update both sides of the relationship. In this case upon creating the OtherClass object you should add it to the objs collection in the User object.

remigio
  • 4,101
  • 1
  • 26
  • 28