我编写了soap服务,其中数据是通过hibernate从数据库中读取的。我在查询中使用JPA Criteria查询和JPA静态元模型来获取数据。我通过hibernate-jpamodelgen插件生成元模型。在处理请求时,当服务创建用于从数据库读取数据的查询时,JPA静态模型类中的主键的SingularAttributes为空(我不知道为什么这些不是popoulated,尽管填充了非主键的其他属性)并且它抛出NullPointerException。
我的课程是:
@MappedSuperclass
public abstract class ReadModel implements Serializable, Cloneable {
public ReadModel() {
}
@Column(name = "customerId")
private BigInteger customerId;
@Id
@Column(name = "device_id")
private BigInteger deviceId;
@Column(name = "date")
private Date date;
@Column(name = "time")
private Time time;
@Id
@Column(name = "device_time")
private Timestamp deviceTime;
public BigInteger getCustomerId() {
return customerId;
}
public ReadModel setCustomerId(BigInteger customerId) {
this.customerId = customerId;
return this;
}
public BigInteger getId() {
return deviceId;
}
public ReadModel setId(BigInteger deviceId) {
this.deviceId = deviceId;
return this;
}
public Date getDate() {
return date;
}
public ReadModel setDate(Date date) {
this.date = date;
return this;
}
public Time getTime() {
return time;
}
public ReadModel setTime(Time time) {
this.time = time;
return this;
}
public Timestamp getDeviceTime() {
return deviceTime;
}
public ReadModel setDeviceTime(Timestamp deviceTime) {
this.deviceTime = deviceTime;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ReadModel that = (ReadModel) o;
return Objects.equals(getId(), that.getId()) &&
Objects.equals(getDeviceTime(), that.getDeviceTime());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getDeviceTime());
}
}
第二节课是:
@Entity
@Table(name = "device_data")
public class DeviceData extends ReadModel implements Serializable,Cloneable {
public DeviceDataRead() {
}
}
相应生成的JPA Static元模型是:
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(ReadModel.class)
public abstract class ReadModel_ {
public static volatile SingularAttribute<ReadModel, Time> time;
public static volatile SingularAttribute<ReadModel, BigInteger> customerId;
public static volatile SingularAttribute<ReadModel, BigInteger> deviceId;
public static volatile SingularAttribute<ReadModel, Timestamp> deviceTime;
public static volatile SingularAttribute<ReadModel, Date> date;
public static final String TIME = "time";
public static final String CUSTOMER_ID = "customerId";
public static final String DEVICE_ID = "deviceId";
public static final String DEVICE_TIME = "deviceTime";
public static final String DATE = "date";
}
并且
@StaticMetamodel(DeviceData.class)
public abstract class DeviceData_ extends ReadModel_ {
public DeviceData_() {
}
}
我的hibernare依赖是:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
<scope>provided</scope>
</dependency>
<properties>
<hibernate.version>5.3.1.Final</hibernate.version>
</properties>
以下是我的实用方法: createProperties()函数返回属性Object。
public class HibernateUtils {
private static SessionFactory factory = null;
public static SessionFactory getSessionFactory(){
if (factory == null) {
factory = buildSessionFactoryFromAnnotatedClasses(HOST, PORT, DB_NAME, PROTOCOL_MY_SQL,
USER_NAME, PASSWORD, CONNECTION_MIN_POOL_SIZE_FOR_PARTICULAR_CLASS_VALUE,
CONNECTION_MAX_POOL_SIZE_FOR_PARTICULAR_CLASS_VALUE, Arrays.asList(DeviceData););
}
return factory;
}
public static SessionFactory buildSessionFactoryFromAnnotatedClasses(String host,
Integer port, String dbName, String protocol, String userName, String password,
Integer minPoolSize, Integer maxPoolSize, List<Class> annotatedClassNames) {
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().applySettings(createProperties(
host, port, dbName, protocol, userName, password, minPoolSize, maxPoolSize)
.getProperties()).build();
MetadataSources sources = new MetadataSources(standardRegistry);
annotatedClassNames.forEach(sources::addAnnotatedClass);
Metadata metaData = sources.getMetadataBuilder().build();
return metaData.getSessionFactoryBuilder().build();
}
}
以下是查询:
ReadModel_.deviceId和ReadModel_.deviceTime都为null,但所有其他属性都已填充(ReadModel_.CUSTOMER_ID,ReadModel_.DATE,ReadModel_.TIME不为空)
try (Session session = HibernateUtils.getSessionFactory().openSession()) {
SingularAttribute<ReadModel, BigInteger> deviceId = ReadModel_.deviceId;//this is null
SingularAttribute<ReadModel, Timestamp> devicetime = ReadModel_.deviceTime;//this is null
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<DeviceData> cq = cb.createQuery(DeviceData.class);
Root<DeviceData> root = cq.from(DeviceData.class);
List<Predicate> conditions = new ArrayList<>();
conditions.add(cb.isTrue(root.get(deviceId).in(devicList)));//root.get(deviceId) throws NullPointerException
conditions.add(cb.greaterThanOrEqualTo(root.get(devicetime), dataStartTime()));
cq.where(conditions.toArray(new Predicate[]{})).orderBy(cb.asc(root.get(devicetime)));
Query query= session.createQuery(cq);
}
我陷入了困境。谁能告诉我该怎么办?提前谢谢。