Reputation: 386
Hi I am new to this world. I am making my own application with spring jpa hibernate. I have an entity class. It works generally but it duplicates attributes. My code look like this:
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
@Entity
public class Event {
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
@Column(nullable = false)
public void setId(Integer id) {
this.id = id;
}
/*------------------------------ title ------------------------*/
private String title;
@Column(nullable = false)
public String getTitle() {
return title;
}
@Column(nullable = false)
public void setTitle(String title) {
this.title = title;
}
/* [Note] I have two extra ids : event_id, eventId
- so these are not the case of the duplication. */
/*------------------- event_id -----------------------------*/
private String event_id;
public String getEvent_id() {
return event_id;
}
public void setEvent_id(String event_id) {
this.event_id = event_id;
}
/*-------------- eventId -------------------------------------*/
@Column(unique = true)
private String eventId;
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
/*------------------------------ publishedDate ------------------------------------------*/
private String publishedDate;
public String getPublishedDate() {
return publishedDate;
}
@Column(nullable = false)
public void setPublishedDate(String publishedDate) {
this.publishedDate = publishedDate;
}
/*--------------------------------- endTime -----------------------------------*/
private String endTime;
public String getEndTime() {
return endTime;
}
@Column(nullable = false)
public void setEndTime(String endTime) {
this.endTime = endTime;
}
/*-------------------------------- user ------------------------------------------*/
@ManyToOne
@JoinColumn(name="user_id")
private User user;
public User getUser() {
return user;
}
@Column(nullable = false)
public void setUser(User user) {
this.user = user;
}
Then when I check JPA diagram, it has duplication of several attributes. I added my JPA diagram image(the red parts are duplicated)
This is my very first application with java programming. so please understand, even I read documentation of the spring jpa, It is difficult to figure out what exactly I did wrong in the code. Any ideas? Thanks in advance.
Upvotes: 3
Views: 2487
Reputation: 52488
Spring Boot 2.0.0.M5, JPA, Hibernate 5.2.11.Final
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "auto_gen")
@SequenceGenerator(name = "auto_gen", sequenceName = "customer_id_seq", allocationSize=1)
@Column(name = "id")
private Long id;
This is a working sample:
package hello;
import org.hibernate.annotations.Type;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "auto_gen")
@SequenceGenerator(name = "auto_gen", sequenceName = "customer_id_seq", allocationSize = 1)
@Column(name = "id")
private Long id;
@Column(name = "first_name")
//@Type(type="org.hibernate.type.StringType")
private String firstName;
@Column(name = "last_name")
//@Type(type="org.hibernate.type.StringType")
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
}
Upvotes: 0
Reputation: 32145
As mentioned in comments, your problem is that you are using two annotations for the same attribute, first in the field and then in its getter/setter, so you have to use only one of them, for example:
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
@Column(nullable = false)
public void setId(Integer id) {
this.id = id;
}
Should be:
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
And it's wrong to usnnotations here with both getter and setter:
@Column(nullable = false)
public String getTitle() {
return title;
}
@Column(nullable = false)
public void setTitle(String title) {
this.title = title;
}
Note:
So in order to avoid this, you have to choose between:
Using annotations with the field.
And using them with its setter.
Upvotes: 1
Reputation: 124441
The problem is that JPA expects either field level annotations or accessor (getters/setters) annotations. However you are using both, which basically isn't allowed. Remove all he annotations from the accessors and only put them on the fields.
Small hint on Java programming (styling) put your fields on top of the classes instead of between the accessors.
@Entity
public class Event {
@Id
@GeneratedValue
private Integer id;
@Column(unique = true)
private String eventId;
private String event_id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String publishedDate;
@Column(nullable = false)
private String endTime;
@ManyToOne
@JoinColumn(name="user_id")
@Column(nullable = false)
private User user;
// Getters / Setters omitted
}
Remove the annotations from the accessors, also do you really want to set the id
? I would expect not, so you might want to remove the setId
method.
Upvotes: 2