【发布时间】:2018-06-06 08:41:55
【问题描述】:
我编写了通过休眠从数据库读取数据的soap服务。我在查询中使用 JPA Criteria 查询和 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() 函数返回属性对象。
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_.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);
}
我被困得很厉害。谁能告诉我我该怎么办?提前致谢。
【问题讨论】:
-
当 JPA 静态元模型为 ... JPA 时,为什么会有大量 Hibernate 本机 API 的负载
-
我不明白你在说什么,你能再解释一下吗? @比利弗罗斯特
-
SessionFactory 和 Session 与 JPA API 无关。当您声称使用JPA API时为什么要使用它????如果您不使用 JPA,请从问题中删除 JPA 标签
标签: java hibernate jpa criteria-api