aspose file tools*
The moose likes Object Relational Mapping and the fly likes OneToMany and save operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToMany and save operation" Watch "OneToMany and save operation" New topic
Author

OneToMany and save operation

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
hi

i use spring data and jpa

i try to save a advertisement, save a message.

when i check message database, advertisement_id is alway null;



sql



my test



any idea?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Mark,

you can try to call flush() on the underlying EntityManager for testing purposes. The spec leaves some room to when exactly the generated ID has to be set by the JPA provider depending on the strategy but flush() should force the ID generation because it requires the JPA provider to synchronize its persistence context with the database. Usually the ID should be generated when a transaction commits successfully but from your code it's not clear how the repository handles transactions for this test.

Oops... I just noticed that your complaint was only because of the foreign key to Advertisement. How exactly are the save() methods on your repositories implemented? Do they start separate transactions for each method call? Have you checked if your entities are detached from the persistence context after each save() method? Maybe that's the problem. More information about the repository implementation and transaction handling would be helpful.

Marco
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Investigate the cascade property of the annotation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Investigate the cascade property of the annotation.

I think cascading is not the problem here because all entities are persisted explicitly (if that is what save() does).
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
save method and so on are genreated by spring data

in only have


same thing for message...

i tried


i get

java.lang.UnsupportedOperationException
at java.util.Collections$1.remove(Collections.java:3313)
at java.util.AbstractCollection.clear(AbstractCollection.java:424)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:511)
at org.hibernate.type.CollectionType.replace(CollectionType.java:667)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:372)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:184)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:157)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:898)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:902)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
at com.sun.proxy.$Proxy29.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:357)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:368)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy30.saveAndFlush(Unknown Source)
at com.klibra.nikela.dao.UserDaoTest.createAds(UserDaoTest.java:88)


so i don't know if i need to save message separately of Advertisement or this last one will save message...

if i want to save message separately, surely i need to add advertisement id in message object...
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

At first glance you message table does not look right for the way your mappings are defined. It should look something like this.. Notice the messages_id




That is assuming status is an enum and you want to store the enum by its ordinal value. I would recommend against that by the way. If you change the order of the enumerated values you are going to have issues. In either event that Status field should be annotated with @Enumerated.

Edit* - For whatever reason the code block is not displaying underscores. It should be:
create table Message (
id bigint not null auto_increment,
creationDate date,
description varchar(255),
sentBy_id bigint,
messages_id bigint,
primary key (id)
);

[How To Ask Questions][Read before you PM me]
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Bill Gorder wrote:At first glance you message table does not look right for the way your mappings are defined. It should look something like this.. Notice the messages_id




That is assuming status is an enum and you want to store the enum by its ordinal value. I would recommend against that by the way. If you change the order of the enumerated values you are going to have issues. In either event that Status field should be annotated with @Enumerated.

Edit* - For whatever reason the code block is not displaying underscores. It should be:
create table Message (
id bigint not null auto_increment,
creationDate date,
description varchar(255),
sentBy_id bigint,
messages_id bigint,
primary key (id)
);


same error
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1649
    
    7

I did not see anything amiss other than that so I set up a small project and ran your exact test with those models and that DDL, with spring-data. The test passed. Is there more to that stack trace? What does your configuration look like? What version of Spring and Hibernate are you using?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OneToMany and save operation