Hi All,
I get the following stack trace when attempting to save an object, I get the following error.
Code:
at org.hibernate.shards.session.ShardedSessionImpl.getShardIdOfRelatedObject(ShardedSessionImpl.java:697)
at org.hibernate.shards.session.ShardedSessionImpl.selectShardIdForNewObject(ShardedSessionImpl.java:660)
at org.hibernate.shards.session.ShardedSessionImpl.save(ShardedSessionImpl.java:637)
at org.hibernate.shards.session.ShardedSessionImpl.save(ShardedSessionImpl.java:767)
at com.cmc.hibernate.shards.main.HibernateShardsMain.main(HibernateShardsMain.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
The line# 697 is the boldfaced
Code:
ShardId getShardIdOfRelatedObject(Object obj) {
ClassMetadata cmd = getClassMetadata(obj.getClass());
Type[] types = cmd.getPropertyTypes();
Please let me know if I am doing anything wrong here. My hibernate main class is as follows wherein I get proper SessionFactory & Session objects.
Code:
public static void main(String[] args) {
HibernateUtil util = new HibernateUtil();
SessionFactory sessionFactory = util.createSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
WeatherReport weatherReport = new WeatherReport();
weatherReport.setContinent("ASIA");
weatherReport.setLatitude(BigDecimal.valueOf(44.43));
weatherReport.setLatitude(BigDecimal.valueOf(44.43));
weatherReport.setTemperature(25);
weatherReport.setReportTime(new Date(new GregorianCalendar().getTimeInMillis()));
session.save(weatherReport);
transaction.commit();
session.close();
}
Below is the util class
Code:
public class HibernateUtil {
public SessionFactory createSessionFactory() {
AnnotationConfiguration prototypeConfig = new AnnotationConfiguration().configure("shard0.hibernate.cfg.xml");
Properties props = prototypeConfig.getProperties();
props.entrySet();
prototypeConfig.addAnnotatedClass(WeatherReport.class);
List<ShardConfiguration> shardConfigs = new ArrayList<ShardConfiguration>();
ShardConfiguration shardConfiguration0 = buildShardConfig("shard0.hibernate.cfg.xml");
ShardConfiguration shardConfiguration1 = buildShardConfig("shard1.hibernate.cfg.xml");
shardConfigs.add(shardConfiguration0);
shardConfigs.add(shardConfiguration1);
ShardStrategyFactory shardStrategyFactory = buildShardStrategyFactory();
ShardedConfiguration shardedConfig = new ShardedConfiguration(
prototypeConfig,
shardConfigs,
shardStrategyFactory);
return shardedConfig.buildShardedSessionFactory();
}
ShardConfiguration buildShardConfig(String configFile) {
Configuration config = new Configuration().configure(configFile);
return new ConfigurationToShardConfigurationAdapter(config);
}
ShardStrategyFactory buildShardStrategyFactory() {
ShardStrategyFactory shardStrategyFactory = new ShardStrategyFactory() {
public ShardStrategy newShardStrategy(List<ShardId> shardIds) {
RoundRobinShardLoadBalancer loadBalancer = new RoundRobinShardLoadBalancer(shardIds);
ShardSelectionStrategy pss = new RoundRobinShardSelectionStrategy(loadBalancer);
ShardResolutionStrategy prs = new AllShardsShardResolutionStrategy(shardIds);
ShardAccessStrategy pas = new SequentialShardAccessStrategy();
return new ShardStrategyImpl(pss, prs, pas);
}
};
return shardStrategyFactory;
}
Below is the Entity class
Code:
@Entity
@Table(name = "WeatherReport")
public class WeatherReport {
@Id
@GeneratedValue(generator = "WeatherReportIdGenerator")
@GenericGenerator(name = "WeatherReportIdGenerator", strategy = "org.hibernate.shards.id.ShardedUUIDGenerator")
@Column(name = "reportId")
private Integer reportId;
@Column(name = "continent")
private String continent;
@Column(name = "latitude")
private BigDecimal latitude;
@Column(name = "longitude")
private BigDecimal longitude;
@Column(name = "temperature")
private int temperature;
@Column(name = "reportTime")
private Date reportTime;
//... getters & setters