Reputation: 374
I'm just getting started trying to use Hibernate to create a PostgreSQL database but keep getting this exception:
org.hibernate.HibernateException: java.lang.IllegalArgumentException: max size attribute is mandatory
It happens when I run my main method on the line where I try to build a session factory but I can't find anything about this exception anywhere online. I'm wondering anyone has any ideas on what could be causing this.
Main Class:
package com.package.ingestor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args){
Student user = new Student();
user.setStudentId(1);
user.setStudentName("Bri Guy");
user.setStudentAge(32);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
Hibernate Object:
package com.bossanova.ingestor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "student", uniqueConstraints={@UniqueConstraint(columnNames={"id"})})
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", length=11, nullable=false, unique=true)
private Integer studentId;
@Column(name = "name", length=20, nullable=true)
private String studentName;
@Column(name="age", length=5, nullable=true)
private Integer studentAge;
public Student() { }
public Student(Integer studId, String studName, Integer studAge) {
this.studentId = studId;
this.studentName = studName;
this.studentAge = studAge;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student= Id: " + this.studentId + ", Name: " + this.studentName + ", Age: " + this.studentAge;
}
}
Config file:
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.bossanova.ingestor.Student"/>
</session-factory>
</hibernate-configuration>
Upvotes: 2
Views: 1498
Reputation: 21
If you are using hibernate-agroal add <type>pom</type> to your dependency
Upvotes: 2
Reputation: 175
I solved this for me by switching my Maven dependency from hibernate-agroal to hibernate-core.
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
Upvotes: 6