静态元模型主键属性未填充(空指针异常)



我编写了 soap 服务,其中数据通过休眠从数据库中读取。我在查询中使用了 JPA 标准查询和 JPA 静态元模型来获取数据。我通过Hibernate-JPAmodelgen插件生成元模型。在处理请求时,当服务创建查询以从数据库中读取数据时,JPA 静态模型类中主键的 SingularAttributes 为空(我不知道为什么不填充这些属性,尽管填充了其他非主键的属性(并且它抛出一个 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静态元模型是:

@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_() {
}
}

我的休眠依赖项是:

<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 均为空,但所有其他属性都已填充 (ReadModel_。CUSTOMER_ID,ReadModel_。日期,ReadModel_。时间不为空(

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

我被困得很厉害。谁能告诉我我该怎么办?提前谢谢。

尝试将@EmbeddedId用于复合键

示例指南

相关内容

  • 没有找到相关文章

最新更新