1

Estoy intentando crear una tabla con una clave compuesta en Spring utilizando Hibernate, lo que sucede es que me saca el siguiente error:

2017-09-14 09:07:55.323 ERROR 9108 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000388: Unsuccessful: create table fieldevents (is_required boolean not null, position integer not null, event_id tinyblob, field_id integer not null, primary key (event_id, field_id))

2017-09-14 09:07:55.324 ERROR 9108 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate : BLOB/TEXT column 'event_id' used in key specification without a key length

El key de event es un int auto incrementable pero no sé por qué me lo está tomando como un tipo tinyblob.

El código que estoy utilizando es el siguiente:

@Entity
@Table(name="Events")
public class Event {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Size(max=200)
    @Column(name="id", length=200)
    private int id;

    private boolean active;

    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
    
    private Date endingDate;
    
    @Size(max=2000)
    private String footer;

    @Size(max=2000)
    private String header;
    
    private String image;
    
    private String logo;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event", fetch = FetchType.LAZY)
    private Set<Mail> mails;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event", fetch = FetchType.LAZY)
    @JsonManagedReference(value="event-Field")
    private Set<FieldEvent> eventfields = new HashSet<>();

    @NotNull
    @Size(max=150)
    private String name;
    
    private Date startDate;

    public Set<FieldEvent> getEventfields() {
        return eventfields;
    }

    public void setEventfields(Set<FieldEvent> eventfields) {
        this.eventfields = eventfields;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getEndingDate() {
        return endingDate;
    }

    public void setEndingDate(Date endingDate) {
        this.endingDate = endingDate;
    }

    public String getFooter() {
        return footer;
    }

    public void setFooter(String footer) {
        this.footer = footer;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public Set<Mail> getMails() {
        return mails;
    }

    public void setMails(Set<Mail> mails) {
        this.mails = mails;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    
}


@Entity
@Table(name="Fields")
public class Field {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String name;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "field", fetch = FetchType.LAZY)
    @JsonManagedReference(value="field-Event")
    private Set<FieldEvent> fieldEvents;

    public Set<FieldEvent> getFieldEvents() {
        return fieldEvents;
    }

    public void setFieldEvents(Set<FieldEvent> fieldEvents) {
        this.fieldEvents = fieldEvents;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

@Entity
@Table(name="fieldevents")
@IdClass(FieldEventId.class)
public class FieldEvent {
    
    @Id
    @ManyToOne
    @PrimaryKeyJoinColumn(name="eventId", referencedColumnName="ID")
    @JsonBackReference(value="event-Field")
    private Event event;
    
    @Id
    @ManyToOne
    @PrimaryKeyJoinColumn(name="fieldId", referencedColumnName="ID")
    @JsonBackReference(value="field-Event")
    private Field field;
    
    private boolean isRequired;
    
    private int position;

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public boolean isRequired() {
        return isRequired;
    }

    public void setRequired(boolean isRequired) {
        this.isRequired = isRequired;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

}

public class FieldEventId implements Serializable{

    private static final long serialVersionUID = 1L;
    
    private Event event;
    
    private Field field;

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }
    
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }


}

1 respuesta 1

-1

Busca algún ejemplo de mykong con estas anotaciones o usa algo tipo @EmbeddedId y @Embeddable (https://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/) y valora que posibilidad te va mejor. Para más info: http://www.objectdb.com/java/jpa/entity/id

Por ejemplo (a falta de saber como tienes el esquema de bbdd):

@Entity
@Table(name="fieldevents")
@AssociationOverrides({
    @AssociationOverride(name = "pk.event",
        joinColumns = @JoinColumn(name = "EVENT_ID")),
    @AssociationOverride(name = "pk.field",
        joinColumns = @JoinColumn(name = "FIELD_ID")) })

public class FieldEvent {

    @EmbeddedId
    private FieldEventId pk = new FieldEventId();

    private boolean isRequired;

    private int position;

    public FieldEventId getPk() {
        return pk;
    }

    public void setPk(FieldEventId pk) {
        this.pk= pk;
    }

    public Field getField() {
        return field;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public boolean isRequired() {
        return isRequired;
    }

    public void setRequired(boolean isRequired) {
        this.isRequired = isRequired;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

}

@Embeddable
public class FieldEventId {

    @ManyToOne
    @PrimaryKeyJoinColumn(name="eventId", referencedColumnName="ID")
    @JsonBackReference(value="event-Field")
    private Event event;

    @ManyToOne
    @PrimaryKeyJoinColumn(name="fieldId", referencedColumnName="ID")
    @JsonBackReference(value="field-Event")
    private Field field;

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

}
2
  • Si bien este enlace puede responder a la pregunta, es mejor incluir las partes esenciales de la respuesta aquí y proporcionar el enlace como referencia. Las respuestas que son únicamente un enlace pueden dejar de ser válidas si la página vinculada cambia. - Desde revisión
    – Shaz
    Commented el 15 sept. 2017 a las 13:13
  • Editado como se sugiere, espero que la respuesta sea válida ahora.
    – Fibman
    Commented el 15 sept. 2017 a las 13:36

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.