Hibernate version: 3.1, Annotations 3.1 beta 8
Problem: Parent object contains children. I want to persist changes from the parent to the child, but not vice versa.
What works: Inserting and deleting of the parent
What breaks: Inserting a new child while updating the parent (child is not inserted)
Code:
Parent:
Code:
...
public class Folder
{
@OnDelete(action=OnDeleteAction.CASCADE)
@OneToMany(cascade=CascadeType.ALL, mappedBy="folder")
private List<FolderTeamPermission> teamPermissions =
new ArrayList<FolderTeamPermission>();
...
Child:
Code:
...
@SequenceGenerator(name="seq", sequenceName="C_Folder_TeamSecurity_Seq")
public class FolderTeamPermission
implements TeamPermission<Folder>
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
private Integer id;
@ManyToOne
@JoinColumn(name="folderId")
private Folder folder;
@ManyToOne
@JoinColumn(name="teamName")
private SecurityTeam team;
...
Business code:
Code:
.. start tx
Folder folder = session.get(Folder.class, folderId);
FolderTeamPermission currTeamPerm = new FolderTeamPermission();
currTeamPerm.setFolder(folder);
currTeamPerm.setPermission(permission);
SecurityTeam team = session.get(SecurityFolder.class, teamName);
currTeamPerm.setTeam(team);
folder.getTeamPermissions().add(currTeamPerm);
folder.setLastModified(new Date());
... commit tx
At this point I have tried just commiting the transaction (which I thought should be enough), "session.saveOrUpdate(folder)" and "session.merge(folder)", but none cause an insert.
All I get in the SQL:
Code:
16:33:18,773 INFO [STDOUT] Hibernate: select users0_.teamname as teamname1_, users0_.username as username1_, cpmuserbea1_.username as username18_0_, cpmuserbea1_.password as password18_0_, cpmuserbea1_.first_name as first3_18_0_, cpmuserbea1_.last_name as last4_18_0_, cpmuserbea1_.active_status as active5_18_0_, cpmuserbea1_.email_address as email6_18_0_ from spappset.S_Users_Teams users0_ left outer join spappset.S_Users cpmuserbea1_ on users0_.username=cpmuserbea1_.username where users0_.teamname=?
16:33:18,805 INFO [STDOUT] Hibernate: update C_Folder set path=?, name=?, parentId=?, owner=?, lastModDt=?, createdDt=? where id=?
The folder is updated as you can see, but the permission that ties the folder to a security team is never inserted even though it was added to the list (Persistent bag).
If I am correct, there should be no reason for me to call "session.persist(currTeamPerm)", as the session should already know I inserted the item.
Is it the "mappedBy" attribute that is the problem, or is there something else I am missing.
I also saw this in the log, not sure if it is making the difference:
Code:
16:33:19,242 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
16:33:19,242 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
16:33:19,242 INFO [SettingsFactory] Scrollable result sets: disabled
16:33:19,243 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
16:33:19,243 INFO [SettingsFactory] Connection release mode: auto
16:33:19,243 INFO [SettingsFactory] Default batch fetch size: 1
16:33:19,243 INFO [SettingsFactory] Generate SQL with comments: disabled
16:33:19,243 INFO [SettingsFactory] Order SQL updates by primary key: disabled
16:33:19,243 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:33:19,243 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
16:33:19,243 INFO [SettingsFactory] Query language substitutions: {}
16:33:19,243 INFO [SettingsFactory] Second-level cache: enabled
16:33:19,243 INFO [SettingsFactory] Query cache: disabled
16:33:19,243 INFO [SettingsFactory] Cache provider: org.hibernate.cache.EhCacheProvider
16:33:19,243 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
16:33:19,243 INFO [SettingsFactory] Structured second-level cache entries: disabled
16:33:19,244 INFO [SettingsFactory] Echoing all SQL to stdout
16:33:19,244 INFO [SettingsFactory] Statistics: disabled
16:33:19,244 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
16:33:19,244 INFO [SettingsFactory] Default entity-mode: pojo
I'm basically stuck. Any help appreciated (I never had this problem with the hibernate.hbm.xml files, so I may just not have mapped the annotations correctly). BTW - I am using the javax.persistence annotations when available and the hibernate annotations only when there are no corresponding javax ones. I am using SessionFactory/Session and not EntityManager to do the persistence.