Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: How do I prevent Hibernate from altering my database table?
PostPosted: Fri Nov 10, 2017 2:30 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 31
Hi,

I have this error :

Quote:
WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1364, SQLState: HY000
2017-Nov-10 14:20:10 PM [http-nio-8013-exec-4] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Field 'tutorQualificationDetail' doesn't have a default value


And I have tried to drop the table and make sure that this field is indeed no default value and it is Not Null etc.

But, after I run my web app, hibernate will just go alter ' my table'.
Another error is causing my subject table field - subjectName to become Varbinary where as in my database it is a Varchar.

Quote:
2017-Nov-10 14:20:09 PM [http-nio-8013-exec-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [13] as [VARBINARY] - [[Ljava.lang.String;@430bcfd9]


Please advise how to prevent all these changes.

Many Tks.


Top
 Profile  
 
 Post subject: Re: How do I prevent Hibernate from altering my database table?
PostPosted: Fri Nov 10, 2017 3:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1515
Add your mappings and the logs that Hibernate generates.

While hbm2ddl is good for generating the initial script, but you should use a tool like Flyway in production.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How do I prevent Hibernate from altering my database table?
PostPosted: Sat Nov 11, 2017 12:22 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 31
vlad wrote:
Add your mappings and the logs that Hibernate generates.

While hbm2ddl is good for generating the initial script, but you should use a tool like Flyway in production.


Hi vlad,

I read that there's a first and second level cache in using Hibernate.

I suspect for my case, Hibernate is caching the old data and I am using hibernate sessionfactory so it is retrieving the 2nd level cache data and thus the error stays the same even though I have created an entire new table manually in MySQL.

Is it possible to erase the cache data at all from somewhere ? like AppData ?


Top
 Profile  
 
 Post subject: Re: How do I prevent Hibernate from altering my database table?
PostPosted: Sat Nov 11, 2017 2:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1515
Unless you explicitly added a 2nd level cache provider and configured your entities to be cached, Hibernate will not use the 2nd level caching mechanism.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: How do I prevent Hibernate from altering my database table?
PostPosted: Sat Nov 11, 2017 2:50 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 31
vlad wrote:
Unless you explicitly added a 2nd level cache provider and configured your entities to be cached, Hibernate will not use the 2nd level caching mechanism.


Alright.

Could you kindly advise me if my mapping is correct ?

Code:
@Entity (name = "tutor")
@Table(name = "tutor")
public class Tutor implements Serializable{
   
   
   /**
    *
    */
   private static final long serialVersionUID = 1L;

   
   @Id
   @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
   @Column(name="tutor_id")
   private int tutor_id;
   
   //public Tutor(String name2, String nRIC2, String email2, String address12, String address22, String zipcode2, String contactNo2, int age2, String qualification2, String qualificationDetail2, String budget2, String remark2) {}
   public Tutor(String name, String NRIC, String gender, String[] subjMany, String address1, String address2, String zipcode, int age, String qualification, String qualificationDetail, String contactNo, String email, String budget, String remark){
      this.name = name;
      this.NRIC = NRIC;
      this.gender = gender;
      this.subjMany = subjMany;
      this.address1 = address1;
      this.address2 = address2;
      this.zipcode = zipcode;
      this.age = age;      
      this.contactNo = contactNo;
      this.email = email;
      this.qualification = qualification;
      this.qualificationDetail = qualificationDetail;
      this.budget = budget;
      this.remark = remark;
   }

The rest of the fields are all the same

@Column(name="tutorQualification")
   @NotNull
   private String qualification;
   
   @Column(name="tutorQualficationDetail", nullable = false)
   @NotNull
   private String qualificationDetail;
   


Thanks again for your help.


Top
 Profile  
 
 Post subject: Re: How do I prevent Hibernate from altering my database table?
PostPosted: Sat Nov 11, 2017 3:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1515
1. The @Id should use a Wrapper not a primitive:

Code:
private Integer tutor_id;


2. If you are using PostgreSQL or any other database that support SEQUENCE objects, you should be using that instead of IDENTITY because, otherwise, you won't be able to batch inserts automatically.

3. Providing a Constructor with more than 4 arguments sounds like a Code Smell. Use the Builder Pattern instead or a Fluent API entity.

4. You still need to provide a no-arg constructor anyway.

5. The VARBINARY column will be used if you provided a Serializable property that's not mapped as an association or as a custom type, like JsonNode for which Hibernate does not have a built-in associated native Type to handle such Object type.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.