Humoyun Ahmad
Humoyun Ahmad

Reputation: 3082

Hibernate @GeneratedValue(name=???): ID gets incremented by five not by one

I am using Hibernate 4 orm and search for my twitter application. The problem is when I start persisting tweets into MariaDB table, id (primary key) gets incremented by five fold:

2
7
12
17
22
27

I tried all Generation Types which are available : SEQUENCE, IDENTITY, TABLE and AUTO. But none of this provided increment by one. I guess I am missing some configuration, otherwise it should provide increment by one.
Here is my Entity class and hibernate.cfg.xml file:

@Entity
@Indexed
@Table(name="tweets_table")
public class TweetPOJO implements Serializable{

private static final long serialVersionUID = 1123211L;

@Id
@GeneratedValue (???)
@Column(name = "raw_id" )
private int raw_id;

@Column(name = "tweet_id")
private String tweet_id;


@DateBridge(resolution=Resolution.DAY)  
@Column(name = "posted_time")
private String timestamp;

@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Column(name = "cleaned_text")
private String tweet_text;


@Column(name = "hashtags")
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.YES)
private String hashtags;

@Column(name = "media_url")
private String media_url;

@Column(name = "media_text")
private String media_text;

@Column(name = "media_type")
private String media_type;

@Column(name = "location")
private String location;

...
...
public void setUser_frind_count(int user_frind_count) {
    this.user_frind_count = user_frind_count;
}

@Override
public String toString() {
    return tweet_id+" : "+tweet_text;
}

}

Here is hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

   <property name="hibernate.bytecode.use_reflection_optimizer"> true </property>
   <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>
   <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url"> jdbc:mysql://*******/clouddata </property>
   <property name="hibernate.connection.username"> root </property>
   <property name="hibernate.connection.password"> qwe123 </property>


   <property name="connection.pool_size"> 1 </property>
   <property name="hibernate.search.default.directory_provider"> filesystem </property>

   <property name="hibernate.search.default.indexBase"> E:\lucene_index </property> 
   <!-- 
     whether the schema will be created or just updated, every time the sessionFactory is created. 
    This is configured in the hibernate.hbm2ddl.auto property, which is set to update. So the schema 
    is only updated. If this property is set to create, then every time we run our application, the 
    schema will be re-created, thus deleting previous data.  
    -->
   <property name="hibernate.hbm2ddl.auto">update</property>
   <mapping class="twitter_crawling_modul.TweetPOJO"/>
</session-factory>

Upvotes: 0

Views: 85

Answers (1)

Mykola Yashchenko
Mykola Yashchenko

Reputation: 5361

You can try to use sequence generator

@SequenceGenerator(name = "seq", sequenceName = "seq_name", allocationSize = 1)
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "seq")

Upvotes: 2

Related Questions