-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Problems with annotaion Id
PostPosted: Fri Sep 02, 2005 3:51 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Hy people, i'm from Brazil and my english is not so good, but i'll try ask the better way. I'm using a hibernate Annotations to map my classes and i'm having problems with insert the classes that @Id is not Generator.AutoType. The hibernate doesn't make the insert and i donĀ“t no why? There is something that i have to do when my classes don't have the Generator.AutoType?

Thanks,

Alberto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 8:25 pm 
Newbie

Joined: Fri Sep 02, 2005 7:36 pm
Posts: 6
Are you from guj?

Well, my english is not so good too. :)

When you try to insert an object, is any exception occurs? Is the show-sql property enabled? The sql is printed in the console?

I had this problem when i've been using the spring framework, because in the unit tests, it rollback which method.




Post your pojo. I'll try to help you


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 5:20 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
Code:
package br.com.lupa.testes;

import java.lang.reflect.InvocationTargetException;

import br.com.lupa.beans.Computador;
import br.com.lupa.beans.Hd;
import br.com.lupa.beans.Processador;
import br.com.lupa.persistencia.hibernate.Dao;

public class AddTest {
   
   public static void main(String args[]) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
      Dao dao;
      Processor p = new Processor();
      Computer c = new Computer();
      Hd hd = new Hd();
      hd.setAvailableSpace("2222222");
      dao = new Dao(Computador.class);
      dao.add(c);         
      dao = new Dao(Processor.class);
      p.setDeviceID("1");//setting the processor id
      p.setCPUStatus("787");
      p.setComputador(c);
      dao.adiciona(p);
   }

}

Bean with annotaions:
Code:
package br.com.lupa.beans;



import javax.persistence.Entity;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table (name="PROCESSOR")
public class Processor {
  private String deviceID;
  private String elementName;
  private String family;
  private String maxClockSpeed;
  private String currentClockSpeed;
  private String cPUStatus;
  private Computer computer;
 


public String getCPUStatus() {
   return cPUStatus;
}
public void setCPUStatus(String status) {
   cPUStatus = status;
}
public String getCurrentClockSpeed() {
   return currentClockSpeed;
}
public void setCurrentClockSpeed(String currentClockSpeed) {
   this.currentClockSpeed = currentClockSpeed;
}
@Id
public String getDeviceID() {
   return deviceID;
}
public void setDeviceID(String deviceID) {
   this.deviceID = deviceID;
}
public String getElementName() {
   return elementName;
}
public void setElementName(String elementaName) {
   this.elementName = elementaName;
}
public String getFamily() {
   return family;
}
public void setFamily(String family) {
   this.family = family;
}
public String getMaxClockSpeed() {
   return maxClockSpeed;
}
public void setMaxClockSpeed(String maxClockSpeed) {
   this.maxClockSpeed = maxClockSpeed;
}
@ManyToOne
public Computer getComputer() {
   return computador;
}
public void setComputer(Computer computer) {
   this.computer = computer;
}
}



Thanks,

Alberto


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 5:23 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
In the method TestAdd, the method is setComputer.
Code:
package br.com.lupa.testes;

import java.lang.reflect.InvocationTargetException;

import br.com.lupa.beans.Computador;
import br.com.lupa.beans.Hd;
import br.com.lupa.beans.Processador;
import br.com.lupa.persistencia.hibernate.Dao;

public class AddTest {
   
   public static void main(String args[]) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
      Dao dao;
      Processor p = new Processor();
      Computer c = new Computer();
      Hd hd = new Hd();
      hd.setAvailableSpace("2222222");
      dao = new Dao(Computador.class);
      dao.add(c);         
      dao = new Dao(Processor.class);
      p.setDeviceID("1");//setting the processor id
      p.setCPUStatus("787");
      p.setComputer(c);
      dao.adiciona(p);
   }

}



Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 6:07 pm 
Newbie

Joined: Fri Sep 02, 2005 7:36 pm
Posts: 6
Try to put in your annotation: @Id(generate=GeneratorType.NONE). This is the annotation, used when your application has the responsability to set the id.

I don't what is the default value, when you don't specify the type of the generator.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 6:24 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
I already put (generate=GeneratorType.NONE) and the problem still there. I'm crazy with this problem. There is another way?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 6:37 pm 
Newbie

Joined: Fri Sep 02, 2005 7:36 pm
Posts: 6
Post your dao. Are commiting the transaction?

What is the database?
If you are using MySQL, what is the table type?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 6:50 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 3:42 pm
Posts: 32
The problem was the flush. I put flush and worked. I had one more doubt man, What annotation i have to put in the class computer to when i will remove a computer everything which has relationship type manyToOne with the computer be delete?
My computer bean: The names are in portuguese
Code:
package br.com.lupa.beans;

import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table (name="COMPUTADOR")
public class Computador {
   private Long id;
   private String ip;
   private String nome;
   //login do administrador
   private String login;
   //senha do administrador
   private String senha;
   private Set<PlacaMae> placaMae;
   private Set<Processador> processador;
   private Set<Hd> hd;
 
   
public Computador(){
   
}
public String getIp() {
   return ip;
}
public void setIp(String ip) {
   this.ip = ip;
}
public String getLogin() {
   return login;
}
public void setLogin(String login) {
   this.login = login;
}
public String getNome() {
   return nome;
}
public void setNome(String nome) {
   this.nome = nome;
}
public String getSenha() {
   return senha;
}
public void setSenha(String senha) {
   this.senha = senha;
}
@Id (generate=GeneratorType.AUTO)
public Long getId() {
   return id;
}
public void setId(Long id) {
   this.id = id;
}
@OneToMany (mappedBy="computador", cascade=CascadeType.ALL)
public Set<Hd> getHd() {
   return hd;
}
public void setHd(Set<Hd> hd) {
   this.hd = hd;
}
@OneToMany (mappedBy="computador", cascade=CascadeType.ALL)

public Set<PlacaMae> getPlacaMae() {
   return placaMae;
}
public void setPlacaMae(Set<PlacaMae> placaMae) {
   this.placaMae = placaMae;
}
@OneToMany (mappedBy="computador", cascade=CascadeType.ALL)
public Set<Processador> getProcessador() {
   return processador;
}
public void setProcessador(Set<Processador> processador) {
   this.processador = processador;
}

}
Is it correct?
Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 7:07 pm 
Newbie

Joined: Fri Sep 02, 2005 7:36 pm
Posts: 6
If want to just delete, try : CascadeType.REMOVE.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.