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 get all files in a directory with exclusion of subdirectories using regular expression?
Spring sql-error-codes.xml does not show correct exception on timeout
Approaches to Android MediaPlayer state mismatch error on isPlaying()?
Issue with Android SQLite cursor while reading table having blob content
Is there a non-jdk implementation for a concurrent weak hash map?
how to immplement dynamic parameter on Jmeter with java
What to do when catching e(rror) in Java
apache.servicemix.bundles.quickfix - Attempting to create standalone NewOrderSingle object throws exception
Printing a message if a char has more than one symbol(using char, not String)
Stop Eclipse to show documents when mouse is hover over words in double quotation
What exactly does Android's #hide annotation do?
Java - Server listens on many ports
Should I use one SQLiteHelper for all SQLiteDatabases in my app or one for each one of them?
no connection between postgresql DB and eclipse
How to change Android status bar color in all activities together
Writing sql query in hibernate

Categories

HOME
compiler-construction
sendgrid
pdf
vbscript
magnific-popup
reflection
fluentd
tinymce
homebrew
sqlite-net-extensions
programming-languages
rdf
angular-ui-bootstrap
django-imagekit
mouse
qore
pheatmap
basic
python-unittest
windows-10-universal
communication
metatrader4
tomcat6
kentor-authservices
apache-cayenne
moonmail
static-libraries
reactive-cocoa
plunker
amazonsellercentral
ml
cx-freeze
language-agnostic
microsoft-r
restful-authentication
claims-based-identity
semantic-analysis
dbclient
libssl
quote
srcset
stormpath
typed.js
commit
rotational-matrices
disassembling
swisscomdev
occlusion
ws-security
hp-ux
texmaker
glew
eclipse-gef
xml-documentation
appcompat
strptime
promela
dandelion
dotnetzip
jxcore
dtexec
flutterwave
jspdf-autotable
crosswalk-runtime
objective-c-swift-bridge
grails-tomcat-plugin
intrusion-detection
mikroc
measures
nsfilemanager
bstr
fputcsv
clang-static-analyzer
qcustomplot
testng-dataprovider
wapiti
energy
hsv
kendonumerictextbox
angular-leaflet-directive
riak-cs
0xdbe
ibaction
directoryservices
phalanger
heisenbug
aapt
htmlcleaner
marmalade
farseer
quantlib-swig
blending
soundtouch
enterprisedb
anonymous-methods
runas
pysimplesoap
armcc
parametric-equations
osql
mercurial-server
stage
blitz++
filtered-index
forums
nsviewanimation
rescale
meego
audio-capture
microsoft-virtualization
ti-dsp
sustainable-pace
w3wp
paul-graham
ntvdm.exe

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