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

ActionBar issue: NPE
How to identify 4th occurance of a character using java regex
Estimate BigDecimal Calculation time
How to use session cache to fire actions on url using groovy/ java
Calendar giving wrong day of week
Recognizing underline and strike through attribute during iText pdf processing
Encrypt files in python and decrypt in android
Java Regex: check if a sentence contains only alphabet and numbers
Parse simple JSON array using JSONArray
Alternative to Java Enum
box2d : camera and body not synced
Why does hamcrest any(Myclass.class) requires casting
Search class by author in IntellijIdea
Getting java.lang.ClassNotFoundException
Unresponsive jbutton requesting concurrency
Can't invoke URL on Android

Categories

HOME
osgi
clips
cloud
coq
hook
openmp
path-finding
google-play
electron
alfresco
reveal.js
imacros
quicklook
task
size
quickfix
circuit
jsprit
quartz-scheduler
netflix
systemc
jtextfield
plunker
java-3d
riot.js
abi
functional-testing
tar
accessor
format-specifiers
angular2-aot
vlsi
cas
tooltipster
rst2pdf
xacml
saas
large-file-upload
google-cloud-nl
microsoft-chart-controls
bluestacks
fog
perlin-noise
webtest
http-digest
suricata
x11-forwarding
preconditions
texmaker
bootstrap-dialog
s
logfiles
hibernate-tools
paxos
alphabet
veracode
fancybox-2
aurelia-validation
forever
tactic
magma
media-player
blacklist
pycaffe
yt-project
google-feed-api
rotativa
savon
adxstudio-portals
multi-level
underscore.js-templating
measures
actionbardrawertoggle
superstack
bluemix-app-scan
bluegiga
ptrace
browser-link
historian
simple-framework
muse
processmodel
remobjects
p2
lustre
c++03
npapi
file-copying
html-helper
zend-route
project-planning
article
contenttype
dml
navigationservice
valueconverter
android-2.2-froyo
intentservice
mcts
listings
coverflow
bulkloader
shellexecute
buster.js
django-nonrel
yui-compressor
android-screen-support
pygit2
flashvars
angularjs-controller
hamiltonian-cycle
frameset
tridion-worldserver
locationlistener
pyinotify
sql-server-profiler
imac
getresponsestream
xsdobjectgen
ctp4
mtj
anti-piracy
spec#
scripting-languages

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