P
P
P_Alexander2018-03-19 20:46:06
PostgreSQL
P_Alexander, 2018-03-19 20:46:06

Why are records not being added to the database?

This error takes off and I do not understand how to solve it Please explain to me how to solve it As I understand it, three tables should be created. According to the code, I declared three users, declared and initialized 3 game objects, and then initialized Users with three Role objects, added User Roles, then added Users to Roles and added Users to Game stat
I tried to go from the main table to the secondary one, and in theory should have worked, help me figure out where I made a mistake!!!

org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table testproj.role add column ID int4 not null]
  at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59)
  at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431)
  at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:449)
  at org.hibernate.tool.schema.internal.SchemaMigratorImpl.migrateTable(SchemaMigratorImpl.java:253)
  at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:170)
  at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60)
  at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134)
  at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101)
  at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:470)
  at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
  at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:170)
  at hibernate.pon.al.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:36)
  at hibernate.pon.al.util.Main.main(Main.java:18)
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" of relation "role" already exists
  at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2422)
  at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2167)
  at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
  at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
  at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
  at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)
  at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)
  at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)
  at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:244)
  at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)
  ... 12 more
Exception in thread "main" java.lang.NullPointerException
  at hibernate.pon.al.util.Main.main(Main.java:19)

Entity classes
@Entity
@Table(name = "userentity", uniqueConstraints = {
        @UniqueConstraint(columnNames = "ID"),
        @UniqueConstraint(columnNames = "NICKNAME")
}, schema ="testproj")
public class UserEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer userID;

    @Column(name = "NICKNAME", unique =  true, length = 100)
    private String nname;

    @Column(name = "FIRSTNAME", unique = false, length = 100)
    private String fname;

    @Column(name = "LASTNAME", unique = false, length = 100)
    private String lname;

    @Column(name = "PASSWORD", unique = false, length = 100)
    private String password;

    @Column(name = "PASSWORDTWO", unique = false, length = 100)
    private String passwordTwo;

//    @Column(name = "ROLE", unique = false, length = 20)
    @ManyToOne
    @JoinColumn(name = "id_role")
    private Role role;


    @OneToMany(mappedBy="userEntity", cascade = CascadeType.ALL)
    private Set<GameStat> gameStats = new HashSet<GameStat>();


@Entity
@Table(name ="role", uniqueConstraints = {@UniqueConstraint(columnNames = "ID")},  schema = "testproj")
public class Role implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private Integer idRole;

    @Column(name = "role", unique = false, nullable = false, length = 20)
    private String role;

    public Role(){}

    public Role(String role) {
        this.role = role;
    }

    @OneToMany(mappedBy="role", cascade = CascadeType.ALL)
    private Set<UserEntity> users = new HashSet<UserEntity>();


@Entity
@Table(name="gamestat", uniqueConstraints = {@UniqueConstraint(columnNames = "game_id")},schema = "testproj")
public class GameStat implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "game_id")
    private Integer gameid;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private UserEntity userEntity;

    @Column(name = "vin", unique = false, nullable = false)
    private int vin;

    @Column(name = "lost", unique = false, nullable = false)
    private int lost;


The class where I call everything and add
SessionFactory sessionFactory =
                HibernateUtil.buildSessionFactory();
        Session session = sessionFactory.openSession();
session.beginTransaction();
            UserEntity u_admin;
            UserEntity u_manager;
            UserEntity u_user;

            GameStat gameStat1 = new GameStat();
            gameStat1.setLost(0);
            gameStat1.setVin(0);


            GameStat gameStat2 = new GameStat();
            gameStat2.setLost(1);
            gameStat2.setVin(1);


            GameStat gameStat3 = new GameStat();
            gameStat3.setLost(2);
            gameStat3.setVin(2);

            Role admin = new Role();
            admin.setRole("admin");
            Role manager = new Role();
            manager.setRole("manager");
            Role user = new Role();
            manager.setRole("user");



            u_admin = new UserEntity("nickadmin", "fadmin", "ladmin", "123","123");
            u_manager = new UserEntity("nickmanager", "fmanager", "lmanager", "123","123");
            u_user = new UserEntity("nickuser", "fuser", "luser", "123","123");

u_admin.setRole(admin);
            u_admin.getGameStats().add(gameStat1);

            u_manager.setRole(manager);
            u_manager.getGameStats().add(gameStat2);

            u_user.setRole(user);
            u_user.getGameStats().add(gameStat3);

            admin.getUsers().add(u_admin);
            manager.getUsers().add(u_manager);
            user.getUsers().add(u_user);

            gameStat1.setUserEntity(u_admin);
            gameStat2.setUserEntity(u_manager);
            gameStat3.setUserEntity(u_user);
session.save(admin);
            session.save(manager);
            session.save(user);
            session.save(u_admin);
            session.save(u_manager);
            session.save(u_user);
            session.save(gameStat1);
            session.save(gameStat2);
            session.save(gameStat3);
session.beginTransaction().commit();


<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testproj</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.username">alexroot</property>
        <property name="hibernate.connection.pool_size">20</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping package="hibernate.pon.al"/>
        <mapping class="hibernate.pon.al.UserEntity"/>
        <mapping class="hibernate.pon.al.Role"/>
        <mapping class="hibernate.pon.al.GameStat"/>

    </session-factory>
</hibernate-configuration>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yerlan Ibraev, 2018-03-20
@mad_nazgul

Judging by the error, hibernate is trying to update the database schema, but it fails.
Try not to update/create schema automatically

<property name="hibernate.hbm2ddl.auto">none</property>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question