I have been on this for serveral nights. I would like to link number in user (one user to many numbers) with user in number (many numbers to one user). I have no luck and need your knowledge. No matter what I do I always get errors with either this or that. A straight answer of what to do will do.
application.properties:
spring.jpa.hibernate.ddl-auto=validate
User entity:
@Entity
@Table(name = "user")
public class Users implements Serializable {
private static final long serialVersionUID = 2323232323L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Number> number;
Number entity:
@Entity
@Table(name = "number")
public class Number implements Serializable {
private static final long serialVersionUID = 1212121212L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(cascade = CascadeType.ALL)
private Users user;
Liquibase:
<createTable tableName="user">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="fk_number" type="BIGINT"/>
</createTable>
<createTable tableName="number">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="user" type="BIGINT"/>
</createTable>
There are 2 issues you need to fix:
In a relational table model, you normally model a many-to-one/one-to-many association with a foreign key column on the many side. So, in your example, you only need a foreign key column on the number table and not on the user table.
If you don't specify a @JoinColumn, Hibernate expects that the name of the foreign key column follows this pattern <name of the attribute that owns association>_<name of the primary key of referenced entity>. In your example, Hibernate expects a number_id column in the number table. You can learn more about association mappings in my Ultimate Guide - Association Mappings with JPA and Hibernate.
It should work, if you keep your entity mappings and use this table definition:
<createTable tableName="user">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
</createTable>
<createTable tableName="number">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="user_id" type="BIGINT"/>
</createTable>
As per my understanding, you will be having a foreign key in number table which will represent the user to whom the number will be associated.
Since you are not mentioning @JoinColumn (the annotatiuon which holds information about foreign key)while specifying the @ManyToOne relationship in your Number Entity, thus, by default jpa is trying to look for the column named user_id in your number table(which is not available)
Just add the annotation @JoinColumn giving proper attributes , it should work.
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="<<name of foreign key in number table>>", refrencedColumnName = "id")
refrencedColumnName tells which column has to referred in the parent entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With