[Resolvido] Duvida HQL Hibernate

2 respostas
brucsantos

Bom dia a todos!

Estou com a seguinte duvida:

Quando faço a pesquisa:

select new Bot(b.identity,b.action,b.closeCase,b.comments,b.name,b.reason) from Bot b

É carregado normal todos os objetos, uso isso em um findAll.

Minha duvida:

meu objeto tem uma chave estrangeira. Como Faço para carregar esse objeto em minha pesquisa? No caso uma lista de objetos “Sector”

minha classe:

@Entity
@Table(name="bot")
public class Bot implements br.com.conrado.j4b.domain.Entity {

	private static final long serialVersionUID = -5884531099677067326L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO) 	
	private Long identity;
	@NotNull
	@Column(length=45, nullable=false, unique=false)	
	private String name;
	private String closeCase;
	@NotNull
	@Column
	private String action;
	@NotNull
	@Column(length=10, nullable=false, unique=false)
	private String reason;
	@NotNull
	@Column(length=20, nullable=false, unique=false)
	private String comments;
	@OneToMany(mappedBy="bot",fetch=FetchType.LAZY,cascade={CascadeType.ALL})
	private Set<Sector> sectors = new HashSet<Sector>();
	
	public Bot() {}
	
	public Bot(Long identity, String action, String closeCase, String comments, String name, String reason) {
		this.identity = identity;
		this.action = action;
		this.closeCase = closeCase;
		this.comments = comments;
		this.name = name;
		this.reason = reason;
	}

Obrigado a todos

2 Respostas

g4j

Na forma que está, tua tabela Bot não tem uma chave estrangeira de Sector. A tabela Sector tem uma chave estrangeira de Bot.

Para carregar a lista de sector quando selecionar um Bot, marque a relação como lazy=false e não use o construtor, e sim somente:

from Bot
brucsantos

NOssa… simples assim! rsrsrsr

Obrigado g4j valew pela ajuda!

So mudeio select não precisei mudar a classe. deixei como lazy e funcionou!

Abraços

Criado 23 de fevereiro de 2010
Ultima resposta 23 de fev. de 2010
Respostas 2
Participantes 2