• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB Casting entities question

 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an entity (EJB3 bean) that extends another entity
i want to cast an entity to the extending entity (i would expect the persistency layer would allow this handle the DTYPE column i use to discriminate) instead i get a classcast exception
while i dont have defined any special requirements for the extending entity (like notnull columns)
what is the way to store/cast the entity as the other type, i dont have access to set the DTYPE value and when i add getters and setters for it JPA/hibernate wont allow it

thanks in advance for your reply!
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander,

Kindly post your code, stack trace and what application server/persistence provider you are using.

Thanks,
Reza
 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reza Rahman wrote:Alexander,

Kindly post your code, stack trace and what application server/persistence provider you are using.

Thanks,
Reza


Hi Reza thanks for your reply. Im using JPA and Hibernate
i deleted my code but just want to know what the requirements are to be able to cast one entity to its subtype and the only diffrence between the to is the discriminatorcolumn / value
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alexander,

Provided the code is correct and you have mapped inheritance, this should go quite smoothly. If you haven't mapped inheritance in JPA and are simply relying on Java inheritance, I'm not quite sure how the persistence provider would behave. There is nothing in the spec prohibiting it.

Thanks,
Reza
 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reza Rahman wrote:Alexander,

Provided the code is correct and you have mapped inheritance, this should go quite smoothly. If you haven't mapped inheritance in JPA and are simply relying on Java inheritance, I'm not quite sure how the persistence provider would behave. There is nothing in the spec prohibiting it.

Thanks,
Reza


this is the inhereting EJB if it helps:



package sensored.ejb.bridge;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

import sensored.ejb.relation.BusinessPartner;

@Entity
@DiscriminatorValue("OEM")
public class OEMBridge extends Bridge {
/**
*
*/
private static final long serialVersionUID = 1L;

private BusinessPartner partner;

public BusinessPartner getPartner() {
return partner;
}

public void setPartner(BusinessPartner partner) {
this.partner = partner;
}

}




and this is the stacktrace:
Caused by: java.lang.ClassCastException: sensored.ejb.bridge.Bridge cannot be cast to sensored.ejb.bridge.OEMBridge
at sensored.core.webservice.RegisterBridgeWS.RegisterBridge(RegisterBridgeWS.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
... 42 more
 
Alexander Curvers
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this are the anotations for the other entity

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"serialNumber", "activationCode"}))
@DiscriminatorValue("YOUW8")
public class Bridge implements Serializable {
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!