简体   繁体   中英

How to insert entity using Hibernate in Amazon Redshift

I'm trying to make a insert into a table using Hibernate. The problem is that Redshift doesn't accept sequences, but instead accept the creation of a IDENTITY for a row.

I have the following scenario:

My table structure:

CREATE TABLE rsds_ops_latam.log( idlog BIGINT PRIMARY KEY IDENTITY(0,1), idaccount varchar(255), username varchar(255), oldowner varchar(255), newowner varchar(255), oldclass varchar(255), newclass varchar(255), date timestamp DEFAULT GETDATE() );

My Entity:

@Entity
@NamedQuery(name="Log.findAll", query="SELECT l FROM Log l")
@Table(name = "log")
public class Log implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "idlog")
private long idLog;

private String userName;

@Temporal(TemporalType.TIMESTAMP)
@GeneratedValue()
@Column(name = "date")
private Date date;

@Column(name = "idaccount")
private String idAccount;

@Column(name = "oldowner")
private String oldOwner;

@Column(name = "newowner")
private String newOwner;

@Column(name = "oldclass")
private String oldClass;

@Column(name = "newclass")
private String newClass;

And the following appears in the log when I try to persist the entity:

11:38:16,702 INFO  [stdout] (default task-3) Hibernate: 
11:38:16,702 INFO  [stdout] (default task-3)     insert 
11:38:16,702 INFO  [stdout] (default task-3)     into
11:38:16,702 INFO  [stdout] (default task-3)         rsds_ops_latam.log
11:38:16,702 INFO  [stdout] (default task-3)         (date, idaccount, newclass, newowner, oldclass, oldowner, userName) 
11:38:16,702 INFO  [stdout] (default task-3)     values
11:38:16,703 INFO  [stdout] (default task-3)         (?, ?, ?, ?, ?, ?, ?)
11:38:18,340 INFO  [stdout] (default task-3) Hibernate: 
11:38:18,346 INFO  [stdout] (default task-3)     select
11:38:18,347 INFO  [stdout] (default task-3)         currval('rsds_ops_latam.log_idlog_seq')
11:38:18,876 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) SQL Error: 500310, SQLState: 42P01
11:38:18,878 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) [Amazon](500310) Invalid operation: relation "rsds_ops_latam.log_idlog_seq" does not exist;

And my persistence.xml is as follow:

<persistence-unit name="accounts2" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:/AccountMovementsRS</jta-data-source>
     <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.default_schema" value="rsds_ops_latam"/>
        <property name="hibernate.connection.url" value="myconnection" />
        <property name="hibernate.connection.username" value="user" />
        <property name="hibernate.connection.password" value="password" />
         <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.flushMode" value="FLUSH_AUTO" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
     </properties>
</persistence-unit>

There is any ideia in what I can do?

Thanks!

Redshift is based on Postgres but it's missing many of Postgres features. One of them are sequences. You can try annotating your idlog column with [ GenerationType.TABLE ]( https://docs.oracle.com/javaee/7/api/javax/persistence/GenerationType.html#TABLE ) . Hibernate should then create a table which will be used similarly to a sequence and should do it in plain sql (compatible with Redshift).

Please Note , there is a performance penalty for that approach as hibernate will lock the sequence table for each insert which may turn out costly.

I also support @John's opinion that Redshift is not meant for single inserts (thus missing features).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM