Entity relation deleated
mhiggins Oct 21, 2007 9:35 AMThis may be an EJB3 question but I am new to both and working with seam ? I have a many to many unidirectional relation. I have a simple from to build these objects / relations up and that works fine. In another form I am trying to work on instances of the object graph but when I display the page the last line in the log is:
09:09:27,174 INFO [STDOUT] Hibernate: delete from Profile_Location where profile_id=? 09:09:27,225 INFO [STDOUT] Hibernate: delete from Profile_Location where profile_id=? 09:09:27,226 INFO [STDOUT] Hibernate: delete from Profile_Location where profile_id=?
I have no delete calls in my code and can't figure out where this is coming from. There are 3 users in the system and that corresponds to the 3 delete calls above. Are my relations mapped wrong? Here is the code for the relations. I have a user and a user has a profile and the profile can have many locations, i.e. a person can have more then one address. The main question is why are these relations being deleted?
User Entity
@Entity @Name("user") public class User implements Serializable { ... private Profile profile; ... @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "profile_id") public Profile getProfile() { return profile; } public void setProfile(Profile profile) { this.profile = profile; } }
Profile Entity
@Entity @Name("profile") public class Profile implements Serializable { ... private Set<Location>locations; .... @ManyToMany @JoinTable(name="Profile_Location", joinColumns={@JoinColumn(name="profile_id")}, inverseJoinColumns={@JoinColumn(name="location_id")}) public Set<Location> getlocations() { return locations; } public void setlocations(Set<Location> locations) { locations = locations; } }
Location Entity. I want the Location to be associated with other types of objects(non just profiles ) there is no reference to the Profile in the Locaiton.
@Entity @Name("location") public class Location implements Serializable { ... }
DistanceCalcAction, this is the statefull session bean that is using the Location entities to calcualte distance between users.
@Stateful @Scope(EVENT) @Name("distanceCalc") public class DistanceCalcAction implements DistanceCalc { @Logger Log log; @PersistenceContext private EntityManager em; private User fromUser; private User toUser; @In private FacesMessages facesMessages; @Out private List<User> users; @Out(required=false) private Double distance; @Create public void getUsers(){ users = (List<User>)em.createQuery("SELECT u from User as u").getResultList(); } public void calc(){ Distance distanceTool = new Distance(); User from = (User) em.createQuery("SELECT u FROM User u WHERE u.id = :id").setParameter("id",fromUser.getId()).getSingleResult(); User to = (User) em.createQuery("SELECT u FROM User u WHERE u.id = :id").setParameter("id",toUser.getId()).getSingleResult(); distance = distanceTool.distanceVincenty( from.getProfile().getDefaultLocation().getLatitude(), from.getProfile().getDefaultLocation().getLongitude(), to.getProfile().getDefaultLocation().getLatitude(), to.getProfile().getDefaultLocation().getLongitude(), Distance.FORMAT_MILES); } @Factory("fromUser") public User getFromUser() { fromUser = new User(); return fromUser; } @Factory("toUser") public User getToUser() { toUser = new User(); return toUser; } public void setFromUser(User fromUser) { this.fromUser = fromUser; } public void setToUser(User toUser) { this.toUser = toUser; } @Destroy @Remove public void destroy() {} }