Hibernate @GeneratedValue(name = ???):ID增加5而不是1

时间:2016-08-02 04:06:38

标签: java hibernate mariadb

我正在使用Hibernate 4 orm并搜索我的twitter应用程序。问题是当我开始将推文持久存入MariaDB表时,id(主键)增加了五倍:

2
7
12
17
22
27

我尝试了所有可用的生成类型: SEQUENCE,IDENTITY,TABLE和AUTO。但这些都没有提供增量。我想我错过了一些配置,否则它应该提供一个增量 这是我的Entity类和hibernate.cfg.xml文件:

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

}

这是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>

1 个答案:

答案 0 :(得分:2)

您可以尝试使用序列生成器

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