0

This answer didn't work. I'm still getting the same exception.

hibernate.cfg.xml

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
  
<property name="hbm2ddl.auto">update</property>
   
<!-- Use Annotation-based mapping metadata -->
<mapping class="entity.Person"/>

These are my versions: pom.xml

<dependencies>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.26</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.5.6.Final</version>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
</dependencies>

Main class

public static void main(String[] args) {
    // HibernateUtil below is my custom Session factory java code
    Session session = HibernateUtil.getSessionFactory().openSession(); 
    Transaction txn = session.getTransaction();
    try {
        txn.begin();
        Address address = new Address("200 E Main st", "Seattle", 211004);
        Person person = new Person("Tanzeel Mirza", address);
        
        session.save(person);
        txn.commit();
    }
    catch(Exception e) {
        //roll back code
    }
    finally {
        if(session!=null) {
            session.close();
        }
    }
}

Person.java

...
@Entity
@Table(name="person")
public class Person {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO) //<----- tried IDENTITY also
    @Column(name="id")
    private long pid;
    
    @Column(nullable=false) 
    private String name;
    
    @Embedded
    private Address address;
    
    // getters and setters and constructor
}

Address.java

@Embeddable
public class Address {
    
    @Column(name="street")
    private String street;

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

    @Column(name="zipcode")
    private long zipcode;
    
    // constructors
}

Here is the complete log:

enter image description here

Please point out my mistake.

Tanzeel
  • 4,174
  • 13
  • 57
  • 110

1 Answers1

0

I tried something from this answer. Though its not the accepted answer as the question is quite different from mine, but it worked for now.

Looks like the problem was the result of conflicting MySQL versions for MySQL 5.x.

So, instead of MySQLDialect I should use MySQL5Dialect in hibernate.cfg.xml:

Solution:

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

That's it!

Tanzeel
  • 4,174
  • 13
  • 57
  • 110