Hibernate Annotations 3.2.0CR1 中文指南

3星 · 超过75%的资源 需积分: 3 7 下载量 38 浏览量 更新于2024-07-25 收藏 406KB DOC 举报
"hibernate_annotations中文帮助文档" Hibernate Annotations 是 Hibernate 框架的一个扩展,它允许开发者使用 Java 注解来定义对象-关系映射(ORM),而不是传统的 XML 配置文件。这个中文帮助文档详细介绍了如何在 Hibernate 中使用注解进行实体 Bean 的映射、查询、缓存以及其他高级功能的配置。 1. 创建一个注解项目: 在开始使用 Hibernate Annotations 之前,你需要满足一定的系统需求,包括 Java 和 Hibernate 的版本。系统配置涉及设置数据库连接、Hibernate 配置文件等内容。 2. 实体Bean: - 简介:实体Bean是 ORM 映射的核心,它们代表数据库中的表。 - EJB3 注解映射:使用 @Entity、@Table 等注解将 Java 类与数据库表关联起来。 - 声明实体bean:使用 @Entity 定义一个 Java 类为实体bean,并可选地使用 @Table 指定对应的数据库表。 - 映射简单属性:使用 @Column 注解映射基本类型属性,@Embedded 用于映射嵌入式对象(组件)。 - 主键属性:@Id 和 @GeneratedValue 用于定义主键及其生成策略。 - 继承关系:@Inheritance 定义类的继承映射策略,如单表、分表、联合子类等。 - 关联关系:@OneToOne、@ManyToOne、@OneToMany 和 @ManyToMany 分别用于一对一、多对一、一对多和多对多的关系映射,@JoinColumn 和 @JoinTable 控制关联的表结构。 - cascading 和 Transitive Persistence:@Cascade 可以指定级联操作,确保关联对象一起被持久化或删除。 - 查询:@NamedQuery 和 @NamedNativeQuery 用于定义预编译的 HQL 和 SQL 查询,方便复用。 3. 通过 XML 覆写元数据: 虽然注解是主要的映射方式,但有时可能需要使用 XML 来补充或覆盖元数据。这涉及到全局、实体、属性和关联级别的元数据覆写。 4. Hibernate 验证器: - Hibernate 验证器提供了一种在持久化之前验证对象的方法,包括内置的验证规则和自定义规则。 - 使用 @NotNull、@Size 等注解可以为领域模型添加约束,以确保数据的完整性。 这个文档详细阐述了 Hibernate Annotations 的各个方面,对于理解和应用 ORM 解决方案具有极高的价值。通过学习这些内容,开发者能够高效地使用注解来管理 Java 对象与数据库之间的映射,提高开发效率并减少出错的可能性。

将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))

2023-07-12 上传