Problem with the OneToMany association

0

I am trying to create an association one to many I have created tables about mysql, as well as entities, but it does not insert data and tried to debug but I can not find where the error may be.

MySql:

CREATE TABLE IF NOT EXISTS 'pubs' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'name' varchar(200) NOT NULL,
'hourFrom' varchar(11) NOT NULL,
'hourTo' varchar(11) NOT NULL,
'days' varchar(20),
'address' int(11) NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS 'addresses' (
'id' bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'street' varchar(200) NOT NULL,
'number' int(11) NOT NULL,
'pub_id' int(11) NOT NULL,
CONSTRAINT fk_pub FOREIGN KEY (pub_id) REFERENCES pubs(id)    
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS 'pub_address';
CREATE TABLE IF NOT EXISTS 'pub_address' (
'id_address' bigint(20) NOT NULL,
'id_pub' int(11) NOT NULL,
UNIQUE KEY 'UK_9uhc5itwc9h5gcng944pcaslf' ('id_address','id_pub'),
KEY 'FK2ex4e4p7w1cj310kg2woisjl2' ('id_pub')
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE 'pub_address'
ADD CONSTRAINT 'FK2ex4e4p7w1cj310kg2woisjl2' FOREIGN KEY ('id_pub') 
REFERENCES 'pub' ('id_pub'),
ADD CONSTRAINT 'FKr38us2n8g5p9rj0b494sd3391' FOREIGN KEY ('id_address') 
REFERENCES 'address' ('id_address');
COMMIT;

Java:

@Entity
@Table(name="PUB", schema = "wheresmybeer")
public class Pub {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id_pub")
private int id;

@Column(name = "name")
private String name;

@Column(name = "hourFrom")
private String hourFrom;

@Column(name = "hourTo")
private String hourTo;

@Column(name = "days")
private String days;

@Column(name = "idimage")
private Long idImage;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Address> addresses = new ArrayList<>();

public Pub() {}

public Pub(String name, String hourFrom, String hourTo, String days, Long idImage, List<Address> addresses) {
    this.name = name;
    this.hourFrom = hourFrom;
    this.hourTo = hourTo;
    this.days = days;
    this.idImage = idImage;
    this.addresses = addresses;
}

public Pub(String name, String hourFrom, String hourTo, String days, List<Address> addresses) {
    this.name = name;
    this.hourFrom = hourFrom;
    this.hourTo = hourTo;
    this.days = days;
    this.addresses = addresses;
}

public Pub(String name, String hourFrom, String hourTo, String days) {
    this.name = name;
    this.hourFrom = hourFrom;
    this.hourTo = hourTo;
    this.days = days;
}


public String getDays() {
    return days;
}

public void setDays(String days) {
    this.days = days;
}

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;
}

public String getHourFrom() {
    return hourFrom;
}

public void setHourFrom(String hourFrom) {
    this.hourFrom = hourFrom;
}

public String getHourTo() {
    return hourTo;
}

public void setHourTo(String hourTo) {
    this.hourTo = hourTo;
}

public List<Address> getAddresses() {
    return addresses;
}

public void setAddresses(List<Address> addresses) {
    this.addresses = addresses;
}

public Long getIdImage() {
    return idImage;
}
public void setIdImage(Long idImage) {
    this.idImage = idImage;
}
}
@Entity
@Table(name = "addresses", schema = "wheresmybeer")
public class Address {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id_address")
private Long id;

@Column(name = "street")
private String street;

@Column(name = "number")
private int number;


public Address() {}

public Address(String street, int number) {
    this.street = street;
    this.number = number;
}

public Long getId() {
    return id;
}

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

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}


public int getNumber() {
    return number;
}

public void setNumber(int number) {
    this.number = number;
}
}

When debugging I can execute the persist () but it does not end successfully when executing the commit ().

Any help is welcome, thank you very much in advance.

Greetings.

    
asked by Gustavo 02.03.2018 в 05:12
source

0 answers