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 set JAVA_HOME in Mac permanently?
LIMIT 1 with CrudRepository
Difference between import java.util.* and java.util.XXXX [duplicate]
Error in function split.default
Extracting source code metadata from compiler
Hamcrest.Matchers: JSON Nested Array
Selectively allocate dimensions for multi-dimensional array at construction
Consume localhost webservice from remote application
Java conventions about primitive types [closed]
Problems with current thread
Disable a KeyTyped event based on its type
PropertyChange Listener for icons
Using JMS, how can I get the maximum message size for a particular destination?
Spring 3.0.4 Not Executing Scheduled-Task Properly
Close JDialog from JavaFx Button
How to avoid multithreded program being blocked due to threads are waiting

Categories

HOME
cakephp
yii2
wso2-am
path-finding
syntax
grep
spagobi
jira
microservices
sql-server-2016
ravendb
ezpublish
enterprise-library-5
convolution
indesign
podio
python-unittest
vifm
collectd
ibm-odm
jsprit
serverless-framework
django-simple-history
viewport
intentfilter
excel-2007
core-text
kudan
php-openssl
claims-based-identity
autosys
crystal-reports-2010
microsoft-sync-framework
qhull
internet-explorer-9
bluestacks
stormpath
xquery-3.0
ioio
kendo-ui-grid
gammu
io-redirection
automake
unobtrusive-validation
fedex
http-redirect
flashair
acoustics
btrace
user-accounts
celery-task
mplayer
worker-thread
knockout-components
magma
iso8601
redundancy
vtigercrm
chord-diagram
heidisql
azure-virtual-network
nsfilemanager
ctest
mongocsharpdriver
testng-dataprovider
pickadate
captivenetwork
wordml
security-testing
rgeo
php-ci
web-controls
e10s
npapi
oxwall
castle
openexr
reactfx
seaside
ember-charts
monomac
jmapviewer
easy-install
cos
chronoforms
osql
external-accessory
deobfuscation
blackberry-playbook
locate
subviews
getresponsestream
ctp4
swing-app-framework
text-coloring
putchar
zune
w3wp

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