jpa


Java EE, casting Entity to Extended one!


Goodmorning.
I have two entitis, User and User1 (which extends User with #Inheritance(strategy=InheritanceType.JOINED), adding some extra information like email, phonenumber and so on... attributes not required for the entity User).
Now... User user is already persisted.
I would like, in future, to be able to cast this user, providing that extra data, doing something like this:
User1 user1 = (User1)user;
user1.setEmail("email#email.com");
I've tried this solution, but of course... it doesn't work!
(Casting Exception Returned).
Any idea? Any help?
Thanks.
Casting in Java doesn't work that way. You can only cast to Objects you actually extend from.
What you could do is provide a Copy Constructor in your extended Entity:
public ExtendedUser(BasicUser user){
this.setFoo(user.getFoo());
this.setBar(user.getBar());
}
But there's no way to cast something into something it doesn't extend. That's called type safety and is one of the most important aspects of Java.
Reference:
Java Language Specification: 5.1.6 Narrowing Reference
Conversions

Related Links

How to use JPA CMT with glassfish 3.1
JPA NamedQuery to obtain a referenced object without executing join
set Java Bean Timeout
JPA not updating ManyToMany relationship in returning result
Working fine without #column specified in the program
Spring data JPA collection pagination total elements
Dynamic schema selection in eclipselink
How to persist a guava hashcode with JPA?
JPA: Unidirectional ManyToMany with existing object to be inserted
spring data lazy loading
JPA Entity with id from referencing class
JPA: implicity persist #ManyToOne entities
modifying the persistence.xml at runtime
Insert object with composite primary key
In JPA, having a many-to-one as primary key throws referential integrity constraint violation
JPA never fetcheable #OneToMany

Categories

HOME
android
beautifulsoup
xml
checkstyle
eslint
merge
jmeter
angular-formly
solver
requirements
missing-data
angularfire2
nfs
pkcs#11
mql5
plist
windows-ce
project-intu
line
datastage
spring-social
plink
package-managers
screen-readers
azure-powershell
red5
wolframalpha
digital-signature
gmm
babel
roslyn
monit
xcode-ui-testing
apktool
genexus-sd
mockjax
dumpbin
playstation-portable
clickjacking
android-sugarorm
keras-layer
hunspell
sql-delete
batching
apic
xcode7.3
google-earth-engine
protein-database
cin
datacachefactory
branching-and-merging
microsoft-metro
http-request
oim
jquery-ui-draggable
aquamacs
modelsim
jqchart
otp
payload
cpu-cache
rxtx
rsa-archer-grc
jboss-tools
jquery-jscrollpane
expectations
mediametadataretriever
grass
httpruntime.cache
dynamics-nav-2016
terracotta
include-guards
pyobjc
nutiteq
git-ftp
ejb-2.x
at-job
boost-program-options
spy++
punycode
personalization
lnk
functional-java
openargs
android-usb
ebay-lms
gpars
node-blade
octal
insert-id
cascadingdropdown
idn
dajaxice
rmiregistry
paginator
httponly
stress
box2d-iphone
update-statement
drawtobitmap
photolibrary
aspmenu
nstokenfield
django-piston
asynchronous-wcf-call
localizable.strings
business-model

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