Ola a todos,
Tenho uma classe que tem chave composta, para isto escrevo duas classes:
@Entity
public class CardTypeZhone {
@EmbeddedId
private CardTypeZhonePK id;
@Column(nullable=false)
private String name;
public CardTypeZhone(CardTypeZhonePK id, String name) {
this.id = id;
this.name = name;
}
public CardTypeZhone() {
}
public CardTypeZhonePK getId() {
return id;
}
public void setId(CardTypeZhonePK id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Classe que tem os campos para fazer a chave composta:
@Embeddable
public class CardTypeZhonePK implements Serializable {
private int typeZhone;
private int lineType;
public CardTypeZhonePK() {
}
public CardTypeZhonePK(int typeZhone, int lineType) {
this.typeZhone = typeZhone;
this.lineType = lineType;
}
public int getLineType() {
return lineType;
}
public void setLineType(int lineType) {
this.lineType = lineType;
}
public int getTypeZhone() {
return typeZhone;
}
public void setTypeZhone(int typeZhone) {
this.typeZhone = typeZhone;
}
}
Até ai tudo bem, criei um dao onde eu preciso recuperar um objeto do tipo CardTypeZhone através do método load passando a classe que compoe a chave composta.
public CardTypeZhone getCardTypeZhone(int cardTypeZhone, int lineType){
CardTypeZhonePK id = new CardTypeZhonePK(cardTypeZhone, lineType);
return (CardTypeZhone) session.load(CardTypeZhone.class, id);
}
Agora vem o problema, em uma classe onde eu preciso recuperar um o objeto CardTypeZhone e então pegar o atributo nome para setar em outro onjeto, porem o objeto voltar CardTypeZhone voltar como nulo, quero cadastro-lo e no meu objeto que tenho que setar o nome deste objeto que veio como nulo vou deixar como unknow, so que esta dando problema, toda vez que ele nao acha o objeto gera uma exececao e nao cadastro o que eu quero, vejam parte do codigo que eu chamo o dao:
DaoCardTypeZhone dao = new DaoCardTypeZhone(HibernateCore.getSession());
int typeZhone = this.getCardType(i);
int lineType = this.getCardLineType(i);
CardTypeZhone cardTypeZhone = dao.getCardTypeZhone(typeZhone, lineType);
if (cardTypeZhone == null) {
try {
dao.getDaoGeneric().save(new CardTypeZhone(new CardTypeZhonePK(typeZhone, lineType)));
card.setName("unknow");
} catch (DaoException ex) {
Logger.getLogger(ZhoneCard.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
card.setName(cardTypeZhone.getName());
}
e agora a execao:
INFO: Not binding factory to JNDI, no JNDI name configured
Exception in thread "pool-1-thread-3" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [br.com.model.CardTypeZhone#br.com.model.CardTypeZhonePK@a50916]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at br.com.model.CardTypeZhone$$EnhancerByCGLIB$$14c90268.getName(<generated>)
at br.com.gvt.zhonelib.system.ZhoneCard.getCards(ZhoneCard.java:78)
at br.com.gvtzhoneinventory.core.ThreadZhoneInventory.persistenceCards(ThreadZhoneInventory.java:43)
at br.com.gvtzhoneinventory.core.ThreadZhoneInventory.run(ThreadZhoneInventory.java:84)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)