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

Problem with CascadeType.ALL and lifecycle callbacks
JPA entity relations are not populated after .persist()
UPDATE and JOIN with JPQL
JPA how to remove parent without delete children?
JPA entityManager is null in Pointcut
JPA: NamedQuery of name: <query> not found
Glassfish EJB 3 OpenJPA 2
owned and unowned relationships
Is there a way to get all managed entities from an EntityManager
Multi-Tenant Seam + JPA Application
JPA mapping error for sequence number
JPA : What is the behaviour of merge with lazy initialized collection?
JPA problem one-to-one association cascade= PERSIST
JPQL we can't CONCAT(String, Integer) EclipseLink?
Cant persist an entity
How to implement self join in JPA?

Categories

HOME
batch-file
url-redirection
prism
fpga
redux-form
static
apiblueprint
graphics
carousel
onclick
polling
python-multithreading
iis-6
syntaxnet
gson
watson-conversation
siddhi
redis-sentinel
xcodebuild
ipmitool
libc
parameter-passing
vuejs
smartclient
breadth-first-search
postscript
visual-studio-extensions
epsilon
jquery-callback
definitelytyped
beautifier
android-maps
failover
panoramas
openwhisk
joomla2.5
python-import
apiary.io
sim-toolkit
parse-tree
imagenet
git-rewrite-history
protein-database
apns-php
chronicle-map
contract
android-checkbox
x++
openal
builder
automapper-5
vugen
java-bytecode-asm
java-6
colorbar
android-testing
skin
objectscript
structuremap4
materialized-path-pattern
rocks
node-request
simplepie
microstation
windows-azure-queues
wso2developerstudio
ng-grid
phpgrid
inf
vhosts
oracle-spatial
slash
rdflib
ansi
actionfilterattribute
code-documentation
cosine-similarity
formview
encryption-asymmetric
mdichild
tinyxml
authlogic
decoupling
dsym
menubar
android-usb
artemis
pstack
subproject
thttpd
inkcanvas
koken
timthumb
event-receiver
tmx
undefined-index
hbm2java
folding
asynchronous-wcf-call
wspbuilder
idatareader

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App