java


org.hibernate.TransientObjectException: how to properly define one-to-one relationship?


I keep getting this exception when saving a compound object. Still cannot figre out how to overcome it..
Here is my mapping:
#Entity
#Table(name = "store_house")
public class StoreHouse implements Serializable {
// constructors
#Id
#OneToOne(fetch = FetchType.EAGER)
#Cascade({org.hibernate.annotations.CascadeType.ALL})
#JoinColumn(name="ING_ID", unique=true, nullable=false, updatable=false)
private Ingredient ingredient;
#Column(name = "QUANTITY")
private double quantity;
}
// getters and setters
Here is DAO method where I get this exception:
#Override
public void insert(StoreHouse sh) {
sessionFactory.getCurrentSession().persist(sh);
}
Here is my test calss:
#Transactional
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"/test-context.xml", "/test-data.xml"})
public class StoreHouseDAOTest {
#Autowired
private SessionFactory sessionFactory;
#Autowired
private StoreHouseDAO storeHouseDAO;
#Autowired
private StoreHouse expectedStoreHouse;
#Test
public void itShouldPerformCRUDSmoothly() {
// CREATE
storeHouseDAO.insert(expectedStoreHouse);
sessionFactory.getCurrentSession().flush();
// READ
StoreHouse actualStoreHouse = storeHouseDAO.getByIngredient(expectedStoreHouse.getIngredient());
assertEquals(actualStoreHouse.getIngredient().getId(), expectedStoreHouse.getIngredient().getId());
// DELETE
storeHouseDAO.delete(expectedStoreHouse);
sessionFactory.getCurrentSession().flush();
StoreHouse emptyStoreHouse = storeHouseDAO.getByIngredient(expectedStoreHouse.getIngredient());
assertNull(emptyStoreHouse);
}
}
The fragment of defined test data:
<bean id="expectedIngredient" class="com.restaurant.model.Ingredient">
<property name="name" value="TestIngredient"/>
<property name="unit" value="expectedUnit"/>
</bean>
<bean id="expectedStoreHouse" class="com.restaurant.model.StoreHouse">
<property name="ingredient" ref="expectedIngredient"/>
<property name="quantity" value="10"/>
</bean>
I feel like I botched when defining Cascading here.. But could you help me to correct it?
I've managed to make it work at the cost of simplifying the model.
#Entity
#Table(name = "store_house")
public class StoreHouse implements Serializable {
// constructors
#Id
#GeneratedValue(generator = "increment")
#GenericGenerator(name = "increment", strategy = "increment")
#Column(name = "SH_ID")
private Long id;
#OneToOne(fetch = FetchType.EAGER)
#Cascade({org.hibernate.annotations.CascadeType.ALL})
#JoinColumn(name="ING_ID", unique=true, nullable=false, updatable=false)
private Ingredient ingredient;
#Column(name = "QUANTITY")
private double quantity;
// getters and setters
}
It actually requieres a separate id field, so I created it. And it statred to work smoothly. Will probably go with it as no other solutions were found.

Related Links

How to change textview text from another activity on button click in android studio?
Three-level nesting in elasticsearch
InvalidSelectorException - Selenium Webdriver support to recognize accent letters
Form validation with Spring Security login page
Python - IPython Notebook fails to find the Java executable
JBehave reuse steps from dependent projects
Spring boot + JPA - SharedEntityManagerCreator opens sessions at startup
Java Two-Dim Array
JAXB XML Required Field Validation
How to handle Activemq's max frame size exception with failover transport
Build fails after updating to gradle 3.3
Correct Threading approach to log analytics
Google Drive API v3 getting root folder Id in java
Rich domain model vs anemic domain model [closed]
Exception in thread “main” java.lang.UnsatisfiedLinkError: no openalprjni in java.library.path
Why i Can't use PDDOCUMENT PDFBOX in java ee project ?

Categories

HOME
activiti
pycharm
lodash
adb
filtering
cmd
youtube-api-v3
alpha
serverspec
opengl-es-2.0
convolution
esper
metatrader4
nano-server
after-effects
apache-metamodel
normalizr
excel-vba-mac
viewport
introduction
zapier
autoconf
tokenize
telerik-reporting
android-widget
p-value
pepper
fifo
fluentvalidation
typo3-6.2.x
elasticsearch-net
log4js-node
jspm
jaxb2
phonegap
oracle-fusion-middleware
google-qpx-express-api
mozilla
google-api-nodejs-client
web-mining
abstract-class
webdriver-manager
sfdc
gulp-sourcemaps
powershell-dsc
janrain
google-closure
wptoolkit
apache-fop
worksheet
windows-mobile-6.5
windows-iot-core-10
akka-cluster
python-cryptography
nbconvert
android-fonts
libpng
radtreelist
connect-by
packagist
kendonumerictextbox
ios8-today-widget
wordml
event-bubbling
generic-programming
ng-animate
titanium-modules
mesa
aapt
hippomocks
comexception
poller
datagridviewcolumn
resty-gwt
geos
eclipse-memory-analyzer
anonymous-methods
pvrtc
browser-detection
dice
javaspaces
jspinclude
regsvr32
wse3.0
subscript
gnu-prolog
nsobject
horizontal-accordion
winbugs14
yslow
infrastructure
mozilla-prism
boost-smart-ptr
ubuntu-9.04
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App