我有这个问题:
org.hibernate.LazyInitializationException:懒得初始化角色集合:mvc3.model.Topic.comments,没有会话或会话被关闭
这是模型:
@Entity
@Table(name = "T_TOPIC")
public class Topic {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name="USER_ID")
private User author;
@Enumerated(EnumType.STRING)
private Tag topicTag;
private String name;
private String text;
@OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
private Collection<Comment> comments = new LinkedHashSet<Comment>();
...
public Collection<Comment> getComments() {
return comments;
}
}
Run Code Online (Sandbox Code Playgroud)
调用模型的控制器如下所示:
@Controller
@RequestMapping(value = "/topic")
public class TopicController {
@Autowired
private TopicService service;
private static final Logger logger = LoggerFactory.getLogger(TopicController.class);
@RequestMapping(value = "/details/{topicId}", method = RequestMethod.GET)
public ModelAndView …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有注释的Hibernate 3.5.2-FINAL来指定我的持久性映射.我正在努力建立应用程序和一组平台之间的关系.每个应用程序都可用于一组平台.
从我所做的所有阅读和搜索中,我认为我需要将平台枚举类保持为实体,并使用连接表来表示多对多关系.我希望关系在对象级别是单向的,也就是说,我希望能够获得给定应用程序的平台列表,但我不需要找出给定平台的应用程序列表.
这是我的简化模型类:
@Entity
@Table(name = "TBL_PLATFORM")
public enum Platform {
Windows,
Mac,
Linux,
Other;
@Id
@GeneratedValue
@Column(name = "ID")
private Long id = null;
@Column(name = "NAME")
private String name;
private DevicePlatform() {
this.name = toString();
}
// Setters and getters for id and name...
}
@Entity
@Table(name = "TBL_APP")
public class Application extends AbstractEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "NAME")
protected String _name;
@ManyToMany(cascade = javax.persistence.CascadeType.ALL)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name = "TBL_APP_PLATFORM", …
Run Code Online (Sandbox Code Playgroud) 因此,在C++/C#中,您可以创建标记枚举以保存多个值,并且在数据库中存储单个有意义的整数当然是微不足道的.
在Java中你有EnumSets,它似乎是一种在内存中传递枚举的好方法,但是你如何将组合的EnumSet输出到一个整数来存储呢?还有另一种方法来解决这个问题吗?
我正在尝试使用hibernate将一组枚举存储到数据库中.
枚举就像是
public enum SomeEnum {
ITEM,
ITEM2,
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的Hibernate模型实体
@Entity
public class TableObject implements BaseObject {
private Long id;
private Set<SomeEnum> someEnumSet;
@Column(name = "TABLE_COLUMN", nullable = true, insertable = true, updatable = true)
@ElementCollection
public Set<SomeEnum> getSectionSet() {
return sectionSet;
}
public void setSectionSet(Set<SomeEnum> sectionSet) {
this.sectionSet = sectionSet;
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为@ElementCollection注释是正确的.'TABLE_COLUMN'列在DB中的类型为CLOB.(甲骨文).
谢谢,亚历克斯.
考虑以下模型
@Entity
// JPA and JAXB annotations here
public class Employee implements Serializable {
// other fields, annotations, stuffs
...
@ElementCollection(fetch = FetchType.LAZY,
targetClass = Address.class)
@CollectionTable(name = "employee_address",
schema = "hris",
joinColumns = @JoinColumn(name = "employee_id",
nullable = false,
referencedColumnName = "employee_id",
foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT)))
protected Set<Address> addresses;
// setters, getters
...
}
@Embeddable
// JAXB annotations here
public class Address implements Serializable {
// fields, setters, getters
}
Run Code Online (Sandbox Code Playgroud)
的Address
类都被注解@Embeddable
注解,和Employee
类具有一个嵌入元件集合addresses
.元素集合fetch …
我有一个实体,它在数据库中有一些BIT字段:
这些字段boolean
使用Hibernate 3.6.9版本映射到其Java类中的字段.这迫使我为每个想要获取的实体列表编写一个接口方法:
List<Entity> listEditables();
List<Entity> listReviewNeeded();
List<Entity> listActives();
Run Code Online (Sandbox Code Playgroud)
或者编写一个通用的接口方法来实现它们的组合:
List<Entity> listEntities(boolean editables, boolean reviewNeeded, boolean actives);
Run Code Online (Sandbox Code Playgroud)
第二个选择看起来更大,但是如果我将来添加另一个字段,则需要修改接口本身(以及与之耦合的每一行代码).
所以我决定将它表达为枚举Set
:
public enum EntityType{
EDITABLE, REVIEW_NEEDED, ACTIVE
}
//That way there's no need to change interface method's signature
List<Entity> listEntities(Set<EntityType> requiredTypes);
Run Code Online (Sandbox Code Playgroud)
有意义的是,枚举匹配我想要实现的,Entity
类型本身应该有它自己的Set<EntityType>
:
public class Entity{
Set<EntityType> entityTypes;
}
Run Code Online (Sandbox Code Playgroud)
然而,而不是我有映射的布尔值在逻辑上匹配Set
.然后我的问题是,有没有办法Set<EntityType> entityTypes
在基于BIT字段的hibernate中进行映射,还是我必须自己管理那些逻辑boolean
?
UPDATE
将它们映射为a Set
意味着使用in
子句查询List的可能性,如果不是,则意味着在我的控制器和模型代码之间进行转换的额外步骤.
Set<EntityType> typesSet = Sets.newHashSet(EntityType.EDITABLE, EntityType.REVIEW_NEEDED); …
Run Code Online (Sandbox Code Playgroud) 我有一个引用枚举类型列表的实体。该列表存储在数据库中,如下所示:
userName role
-----------------------
user0001 role1
user0001 role2
user0001 role3
user0002 role1
Run Code Online (Sandbox Code Playgroud)
对应的java类大致是这样的:
@Entity
@Table(name = "UserTable")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId")
private Integer id;
@Column(name = "user_name")
private String userName;
@ElementCollection(targetClass = Role.class)
@CollectionTable(name = "User_Roles")
@Column(name = "role")
@Enumerated(EnumType.STRING)
private List<Role> roles;
}
Run Code Online (Sandbox Code Playgroud)
我检查了这个和这个问题的映射。然而我有两个问题。第一个问题是这userName
不是我的 PK,UserTable
据我所知,hibernate 确实加入了 PK。
第二个问题是当前设置的错误:
org.hibernate.LazyInitializationException:无法延迟初始化角色集合:com.project.Common.User.roles,无法初始化代理 - 没有会话
此错误应通过EAGER
加载来修复,但是当我尝试该错误时,在启动时出现以下错误:
java.lang.IllegalStateException:尝试针对查询空间 uid [<gen:1>] 注册多个 SQL 表别名 [roles1_、roles2_ 等]
我需要更改什么才能使此映射正常工作?
请注意,如果可能的话,我不想执行任何数据库更改。 …
在下面的代码中,我有一个shopType
实际上是 a 的字段enum
,在我的场景中,一家商店有多种类型,例如商店 ABC 的类型为杂货店和药房,所以我想enum
在数据库中将列表存储在单独的表中,其中两个栏目中存在一种是,shop_id
另一种是,shop_type
这样一个商店可以有多种类型,我该怎么做?
这是我的代码
商店详情.java
@Entity
public class ShopDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
private Double latitude;
private Double longitude;
private float rating;
private Time openingTime;
private Time closingTime;
@Enumerated(EnumType.STRING)
private Collection<ShopType> shopType;
@Column(columnDefinition=" bit(1)default 1")
private boolean shopEnabled = true;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
店铺类型
public enum ShopType {
GROCERY,
PHARAMACY
}
Run Code Online (Sandbox Code Playgroud) java ×8
hibernate ×6
enums ×3
jpa ×3
spring ×3
annotations ×1
flags ×1
jpql ×1
jsp ×1
mapping ×1
mysql ×1
persistence ×1
spring-data ×1
spring-mvc ×1