休眠抛出最大大小属性是强制性异常

时间:2018-11-28 16:29:22

标签: java postgresql hibernate

我刚开始尝试使用Hibernate创建PostgreSQL数据库,但一直出现此异常:

org.hibernate.HibernateException: java.lang.IllegalArgumentException: max size attribute is mandatory

当我在尝试建立会话工厂的行上运行我的main方法但在网上的任何地方都找不到有关此异常的信息时,就会发生这种情况。我想知道有人对导致这种情况的想法有什么想法。

主类:

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();
    }
}

休眠对象:

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;
    }
}

配置文件:

<!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>

2 个答案:

答案 0 :(得分:1)

如果您使用的是休眠农业,请在您的依赖项中添加 pom

答案 1 :(得分:0)

我通过将Maven依赖项从休眠农业模式更改为休眠核心模式来解决了这个问题。

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.0.Final</version>
</dependency>