我使用Hibernate 3.5.6和MySQL 5.1.我有3个类:Item,Attribute,AttributeValue.它们以一对多的关系彼此相关,我确实得到了给定项目的所有属性.但我没有得到给定属性的AttributeValues.我看不到异常或错误,表中的数据看起来很好,这意味着数据的插入进展顺利.
我为Attribute的AttributeValues映射的FetchMode尝试了不同的策略,但结果总是为空.
我错过了什么?
以下是课程:
@Entity
@Table(name = "Item")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String serialNumber;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "item", cascade = CascadeType.ALL)
@Fetch(value = FetchMode.SELECT) // don't remove this line
@MapKey(name = "name")
private Map<String, Attribute> attributes = new HashMap<String, Attribute>();
protected Item() {
super();
}
public Item(String serialNumber) {
this();
setSerialNumber(serialNumber);
}
public String getSerialNumber() {
return serialNumber;
}
protected void setSerialNumber(String serialNumber) {
this.serialNumber …Run Code Online (Sandbox Code Playgroud) 我对此有点迷失,因为它通常是开箱即用的。我正在制作一个小型 java spring-boot Rest api,为了获得漂亮的 API 描述和测试页面,我使用 Swagger。除了这次它不显示我的应用程序的内容,它显示默认的宠物商店页面。
这是我在 pom 中得到的内容(在我的其他应用程序中看起来是一样的):
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency> …Run Code Online (Sandbox Code Playgroud) 我有两个类:A和B这样B扩展A.使用hibernate,A是一个普通的具体实体,有一个关联的表,一切都很好.
定义B使得它扩展A,A和B是Serializable,并且有2个额外的成员,我有@Transient注释.我不希望将这些保存在数据库中,对于我所关心的所有内容,对我来说,该表中的所有行都只是A的实例,持久化.目标是偶尔在会话中保存B实例.但是在某些时候我需要在DB中保存B对象(就好像它是A).我没有或不需要DiscriminatorColumn或类似的东西.
我尝试过这样的事情:
session.saveOrUpdate((A) b);
Run Code Online (Sandbox Code Playgroud)
...用b实例B.这失败了.
任何人都知道如何解决这个问题?
java ×3
hibernate ×2
inheritance ×1
maven ×1
mysql ×1
one-to-many ×1
save ×1
spring-boot ×1
swagger-ui ×1
transient ×1