Dificuldade com Annotations e FK [RESOLVIDO]

Pessoal,

Estou com dificuldade em relação a annotations e gostaria de uam força de vcs. Eu tenho as seguintes tabelas.

CLIENTE
id_cliente
nome_cliente

NEWSLETTER
id_newsletter
data_newsletter
id_cliente (FK_Newsletter_Cliente)

Se o pessoal tiver alguma documentação(link) de estudo fico grato!

Documentação do hibernate te ajudará.
hibernate reference

Bons estudos

[quote=thiagomoraes]Documentação do hibernate te ajudará.
hibernate reference

Bons estudos[/quote]

Thiago,

Não seria @OneToMany ?

Isso.

Utilize o mapeamento bidirecional, especificando a Join column no owning side e mapped by do outro lado.

@Entity
public class Cliente {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id_cliente")
	private Long idCliente;
	@Column(name="nome_cliente")
	private String nome;
@OneToMany(targetEntity=Newsletter.class ,cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "cliente",orphanRemoval=true)	
	public List<Newsletter> newsletters; 
// getters, setters, construtor vazio e construtor preenchido.
@Entity
public class Newsletter {
@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id_newsletter")
	private Long idNewsletter;
@Column(name="data_newsletter")
	@Temporal(value=TemporalType.DATE)
	@DateTimeFormat(pattern="dd/MM/yyyy")
	private Date data;
@ManyToOne
	private Cliente cliente;	
// getters, setters, construtor vazio e construtor preenchido.

Veja se é isso.

Faltou o @JoinColumn

@ManyToOne @JoinColumn(name="id_cliente") @private Cliente cliente;

Pois o valor default é a concatenação do entity da relacao com _ e mais o nome da propriedade.

[quote=Guevara][quote]
CLIENTE
id_cliente
nome_cliente

NEWSLETTER
id_newsletter
data_newsletter
id_cliente (FK_Newsletter_Cliente)
[/quote]

@Entity
public class Cliente {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id_cliente")
	private Long idCliente;
	@Column(name="nome_cliente")
	private String nome;
@OneToMany(targetEntity=Newsletter.class ,cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "cliente",orphanRemoval=true)	
	public List<Newsletter> newsletters; 
// getters, setters, construtor vazio e construtor preenchido.
@Entity
public class Newsletter {
@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id_newsletter")
	private Long idNewsletter;
@Column(name="data_newsletter")
	@Temporal(value=TemporalType.DATE)
	@DateTimeFormat(pattern="dd/MM/yyyy")
	private Date data;
@ManyToOne
	private Cliente cliente;	
// getters, setters, construtor vazio e construtor preenchido.

Veja se é isso.[/quote]

Guevara,

Me aparece este erro agora:

exception

javax.servlet.ServletException: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [br.ulbra.lpw.classes.Cliente.newsletter#4]
root cause

javax.el.ELException: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [br.ulbra.lpw.classes.Cliente.newsletter#4]
root cause

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [br.ulbra.lpw.classes.Cliente.newsletter#4]
root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'newsletter0_.cliente_id' in 'field list'

Se vc colocou @JoinColumn(name=“id_cliente”) na classe Newsletter, remove e testa de novo.
Eu aqui uso apenas a anotação @ManyToMany sem o join.

“The name of the foreign key column is the name of the referencing relationship field or property + “_” + the name of the referenced primary key column”

Partindo desta afirmação, vamos analisar a necessidade do JoinColumn.

Repare no erro
-> newsletter0_.cliente_id

E na tabela

NEWSLETTER
id_newsletter
data_newsletter
id_cliente (FK_Newsletter_Cliente)

Neste caso, precisamos ou não ?

Eu não crio o banco na mão, uso JPA2 e deixo o Hibernate cuidar disso, o código que postei é o que eu uso aqui e não há problemas com FK, cabe ao autor do tópico decidir se deixa o Hibernate cuidar de td ou se faz o banco na mão tendo que cuidar das FK.

Guevara e Thiago,

Primeiramente obrigado pela ajuda, estou iniciando na tecnologia e algumas coisas ficam meio complexas para mim. Em relação ao comentado pelo Guevara, eu crio na mão, pois, estou conhecedo a tecnologia. Tens algum tutorial de como fazer com o JPA2?

Vou colocar como resolvido o tópico, mas, gostaria que você me indicasse algum material.

Abraço a todos e obrigado novamente!

Infelizmente não tenho muito pra mandar, pois o que eu tenho eu consegui com o pessoal do fórum que têm me ajudado muito.
Veja ai:
http://www.edsongoncalves.com.br/2010/02/03/jpa-2-0-na-pratica-parte-2/


O principal é vc deixar o seu persistence.xml assim:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
			 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
			 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>  
        <properties>        	
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/meubanco" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.password" value="12345" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>           
        </properties>
</persistence-unit>
</persistence>

Este arquivo fica em /src/META-INF/persistence.xml
Eu uso Postgre, se vc usar outro banco é só setar nas respectivas linhas.
Boa sorte!