I’m currently working on an assigement for a class. The Idea is to build a simple Twitter-like Back- and Frontend. For this projekt we got a prebuild projekt with three maven modules:
- buisness (the actual API)
- persistence (the models)
- presentation (the Website)
This project uses h2 as a DB for hibernate to make development easy.
The prebuild stuff works pretty nice, but for your own service I defined this Model of a Post:
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.persistence.*;
import java.util.Date;
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class DBPost extends DBIdentified {
private String message;
private String author;
@Temporal(TemporalType.TIMESTAMP)
@JsonbDateFormat(value = JsonbDateFormat.TIME_IN_MILLIS)
private Date creationDate;
public DBPost() {
}
}
DBIdentified:
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class DBIdentified {
private long id;
@Id
@GeneratedValue
public long getId() {
return this.id;
}
public void setId(final long id) {
this.id = id;
}
}
StartupBean:
@PostConstruct
public void startup() {
final DBPost firstPost = this.entityManager.find(DBPost.class, 1L);
// only initialize once
if (firstPost == null) {
final DBPost post = new DBPost();
post.setUrl("https://dl.gitea.io/gitea/1.8.2/");
post.setAuthor("Gitea");
post.setUrgent(false);
post.setVersion("1.8.2");
post.setCreationDate(new Date());
post.setMessage("BUGFIXESn" +
"n" +
" Fix possbile mysql invalid connnection error (#7051) (#7071)n" +
" Handle invalid administrator username on install page (#7060) (#7063)n" +
" Disable arm7 builds (#7037) (#7042)n" +
" Fix default for allowing new organization creation for new users (#7017) (#7034)n" +
" SearchRepositoryByName improvements and unification (#6897) (#7002)n" +
" Fix u2f registrationlist ToRegistrations() method (#6980) (#6982)n" +
" Allow collaborators to view repo owned by private org (#6965) (#6968)n" +
" Use AppURL for Oauth user link (#6894) (#6925)n" +
" Escape the commit message on issues update (#6901) (#6902)n" +
" Fix regression for API users search (#6882) (#6885)n" +
" Handle early git version's lack of get-url (#7065) (#7076)n" +
" Fix wrong init dependency on markup extensions (#7038) (#7074)n");
this.entityManager.persist(post);
}
}
When I try to insert a Post in the DB (in a Bean of the buisness module) I get this error message:
Caused by: org.h2.jdbc.JdbcSQLException: Value too long for column "MESSAGE VARCHAR(255)": "STRINGDECODE('BUGFIXESnn Fix possbile mysql invalid connnection error (#7051) (#7071)n Handle invalid administrator use... (790)"; SQL statement:
I guess I need to change the type of message in the DB to something like TEXT
or LONGTEXT
.
But all these ways to annotate private String message
failed:
@Type(type="text")
@Column(length= Integer.MAX_VALUE)
@Column(columnDefinition="CLOB")
@Column(columnDefinition="TEXT")
@Column(length=65535, columnDefinition="Text")
@Lob
Could the problem be with the different modules and that the persistence module gets included in the buisness module? Or am I simply doing something wrong with regards to hibernate?
2
Answers
After some search I got the answer:
In
DBIdentified
the getter was annotated and inDBPost
the field was annotated. Mixing of field and getter annotations does not work, because hibernate will search for the @Id annotation and assume everything to be like it.so the fix was
DBIdentified
:It is the annotation you need :