我刚开始尝试使用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>

最佳答案

我通过将我的Maven依赖项从hibernate Agrol切换到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>

关于java - hibernate 抛出最大大小属性是强制性异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53524035/

10-15 21:04