Tengo dos tablas que son asi miren
AMIGOS
---------------------------------------------------------------
id:Integer | usuario_amigo:String | tipo_amigo_id: Integer
---------------------------------------------------------------
1 | panchito | 1
--------------------------------------------------------------
2 | juancito | 2
--------------------------------------------------------------
TIPO_DE_AMIGO
----------------------------
id: Integer | detalle
----------------------------
1 | amigo
----------------------------
2 | super amigo
-----------------------------
La relacion es de muchos a uno, muchos AMIGOS pueden ser de un tipoDeAmigo y un TIPO_DE_AMIGO tiene muchos AMIGOS.
Consideracion: La tabla amigos permite la creacion de mas registros pero la tabla TIPO_DE_AMIGO no tiene permitido agregar mas amigos.
Lo que yo quiero hacer es eso justamente agregar mas amigos a la tabla AMIGOS, respetando esa relacion de uno a muchos.
Esto es lo que tengo de codigo hasta ahora.
CLASE AMIGO
@Entity
@Table(name="AMIGOS")
public class Amigo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Integer id;
@Column(name=nombre_amigo)
private String nombreAmigo;
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="tipo_amigo", nullable=false)
private TipoAmigo tipoAmigo;
public Amigo() {
}
public Usuario(String nombreAmigo, TipoAmigo tipoAmigo) {
super();
this.nombreAmigo = nombreAmigo; //bla bla bla...
}
CLASE TIPOAMIGO
@Entity
@Table(name="TIPO_DE_AMIGO")
public class TipoAmigo {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer id;
@Column
private String detalle;
@OneToMany(mappedBy="tipoAmigo", cascade=CascadeType.ALL)
private List<Amigo> amigos = new ArrayList<Amigo>();
public TipoAmigo (){
}
//Y los demas ...
ESTE ES EL TEST CASE QUE ESTOY TRATANDO DE HACER, EL PATRON DE DISEÑO USADO FUE DAO
public class AmigoTestCase(){
@Test
public void testAgregar() {
TipoAmigo tipoAmigo = new TipoAmigol(1,"amigo");
Amigo nuevo = new Amigo("lucho",tipoAmigo);
int resultado = amigoDao.get().size();
try {
amigoDao.insert(nuevo);
assertFalse(amigoDao.get().size()==resultado);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Esto me salta, talvez no aplico bien las anotaciones o no se bien como se hace esto de las relaciones de muchos a uno y viceversa, necesito ayuda. :huh:
org.hibernate.exception.SQLGrammarException: could not execute statement