我使用Hibernate 5.2.12和MySQL 5.7.18。
在我的项目中有以下实体:
package my.package;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Type;
import pl.chilldev.commons.db.AbstractEntity;
import pl.chilldev.commons.db.timestampable.Timestamp;
import pl.chilldev.commons.db.timestampable.TimestampType;
import pl.chilldev.commons.db.timestampable.TimestampableListener;
/**
* Recording meta entity.
*/
@Entity
@Table(name = "Recordings")
@EntityListeners(TimestampableListener.class)
@Accessors(chain = true)
@Getter
public class Recording extends AbstractEntity
{
/**
* Assigned shop.
*/
@Column(length = AbstractEntity.LENGTH_UUID, nullable = false)
@NotEmpty
@Setter
private UUID idShop;
/**
* Track title.
*/
@Column(length = AbstractEntity.LENGTH_STRING_COMPACT, nullable = false)
@Setter
private String name;
/**
* Track description.
*/
@Column
@Type(type = "text")
@Setter
private String content;
/**
* Creation date.
*/
@Column(nullable = false, updatable = false)
@Timestamp(TimestampType.CREATE)
private OffsetDateTime createdAt;
/**
* Track price.
*/
@Column(nullable = false)
@Setter
private double price;
/**
* Thumbnail S3 key.
*/
@Column(length = AbstractEntity.LENGTH_URL)
@Setter
private String thumbnail;
/**
* Media file S3 key.
*/
@Column(length = AbstractEntity.LENGTH_URL)
@Setter
private String media;
/**
* Length of recording.
*/
@Column
@Setter
private Short duration;
/**
* Categories to which recording is assigned.
*/
@ElementCollection
@CollectionTable(name = "RecordingsCategories", joinColumns = @JoinColumn(name = "idRecording"))
@Column(name = "idCategory", length = AbstractEntity.LENGTH_UUID, nullable = false)
private List<UUID> categories;
}
(AbstractEntity仅定义UUID类型的一个id
属性。)
当我保存实体时(我使用Spring Data JPA和Spring Data REST,所以我只调用端点)一切正常。
但无论出于何种原因,Hibernate都不想从数据库中加载idShop
属性。当我访问加载实体的某个端点时,idShop
始终为null。我试图确定它并且似乎在Hibernate中(与Spring,Spring Data等无关)。
以下是MySQL控制台的打印输出:
mysql> select *, hex(idShop) from Recordings;
+------------------+---------+---------------------+----------+------------------+-------+------+-------+-----------+----------------------------------+
| id | content | createdAt | duration | idShop | media | name | price | thumbnail | hex(idShop) |
+------------------+---------+---------------------+----------+------------------+-------+------+-------+-----------+----------------------------------+
| �HG_OƵ�㰅aY | NULL | 2018-04-04 23:36:46 | NULL | �߈'��C�����9K� | NULL | Test | 2 | NULL | A6DF8827B2F043B4BBE7DADA394B11D2 |
+------------------+---------+---------------------+----------+------------------+-------+------+-------+-----------+----------------------------------+
1 row in set (0,00 sec)
在调试期间,idShop属性的值被大量加载,但结果集的某些wasNull
属性求值为true
并导致整个求值也变为null:
表(由Hibernate自己创建):
mysql> SHOW CREATE TABLE Recordings;
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Recordings | CREATE TABLE `Recordings` (
`id` binary(16) NOT NULL,
`content` longtext,
`createdAt` datetime NOT NULL,
`duration` smallint(6) DEFAULT NULL,
`idShop` binary(16) NOT NULL,
`media` varchar(1024) DEFAULT NULL,
`name` varchar(128) NOT NULL,
`price` double NOT NULL,
`thumbnail` varchar(1024) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)