我有一个由下表组成的模式。
1)表category_type
。基本上我们有4个category_type
一)杂货
b)中制药
c)婴儿用品
d)体育项目
CREATE TABLE `category_type` (
`id` int(11) NOT NULL DEFAULT '0',
`name` varchar(45) NOT NULL,
`image` varchar(128) NOT NULL,
`creation_date` datetime NOT NULL,
`last_updated_date` datetime NOT NULL,
`created_by` int(11) DEFAULT NULL,
`last_updated_by` int(11) NOT NULL,
`object_version_number` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
2)表category
。表的描述如下。
category_type
表和category
表是不同的。类别类型表是前面解释的更高级别。 category
表包含下一级别的分类。例如Staples
,Personal Care
,Biscuit
和Beverages
是属于category_type Groceries
主要类别的第二级别。
所有下一级别的分类都在同一category
表中完成。假设rice and pulses
,flour
,spices
,salt and sugar
是第3级分类,属于主要类别。所以,所有第3级分类都指的是第2级使用" parent_category_id"的类别同一表中的字段。类似地,第四级分类是指使用" parent_category_id"的第三级。同一个表中的字段。
例如:
1)。第一级分类是杂货,制药,婴儿用品,运动项目
2)。第二级分类是rice and pulses
,flour
,spices
,salt and sugar
使用杂货。同样,制药,婴儿用品,运动用品也有自己的第二级分类。
3)。同样,每个第二级别的类别都有自己的第三级别类别。
表格信息如下。
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`creation_date` datetime NOT NULL,
`last_updated_date` datetime NOT NULL,
`created_by` int(11) NOT NULL,
`last_updated_by` int(11) NOT NULL,
`object_version_number` int(11) NOT NULL,
`image` varchar(128) DEFAULT NULL,
`category_type_id` int(11) NOT NULL,
`parent_category_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_category_categorytype_id` (`category_type_id`),
KEY `FK_parent_category_id` (`parent_category_id`),
CONSTRAINT `FK_category_categorytype_id` FOREIGN KEY (`category_type_id`) REFERENCES `category_type` (`id`),
CONSTRAINT `FK_parent_category_id` FOREIGN KEY (`parent_category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1011 DEFAULT CHARSET=utf8
3)TABLE product
。现在最终产品表来了。它有category_id
字段,引用表id
中的category
字段
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`manufacturer` varchar(45) DEFAULT NULL,
`unique_name` varchar(45) DEFAULT NULL,
`barcode` varchar(45) DEFAULT NULL,
`search_code` varchar(45) DEFAULT NULL,
`active` int(1) NOT NULL DEFAULT '1',
`image` varchar(128) DEFAULT NULL,
`creation_date` datetime NOT NULL,
`last_updated_date` datetime NOT NULL,
`created_by` int(11) NOT NULL,
`last_updated_by` int(11) NOT NULL,
`object_version_number` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNQ_product_unique_name` (`manufacturer`,`name`,`category_id`),
KEY `FK_product_category_id` (`category_id`),
CONSTRAINT `FK_product_category_id` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1834 DEFAULT CHARSET=utf8
我面临的问题是我不是写一个与之相关的hibernate查询,我必须获取Staples
或Personal Care
或Biscuit
下的所有数据是第二级别的类别,而产品是由第4级别的类别引用。
请帮帮我。
categoryType
表的样本数据如下
Id name images
1 GROCERY /images/customer/groceries-icon.jpg
2 PHARMA /images/customer/pharma-icon.jpg
3 GIFTS /images/customer/gifts-icon.jpg
4 TOYS /images/customer/toys-icon.jpg
category
表的样本数据如下
Id Name Category_type_id parent_Category_id
53 Staples 1 Null
54 Dry fruits 1 53
55 Salt and Sugar 1 53
56 Pasta,Noodles 1 77
57 Pickles.Sauce 1 77
58 BUSCITS 2 Null
59 Ready to eats 1 77
60 Oil and Ghee 1 53
61 Flour And Suji 1 53
62 Rices and Pulses 1 53
63 TOILETERS 2 Null
64 KIMIS NOODLES 2 Null
65 KWALITY 2 Null
66 HouseHold 1 Null
67 Wash and Clean 1 Null
68 Biscuit 1 Null
69 Skin Care 1 74
74 Personal Care 1 Null
75 Babies Cares 1 74
77 Packed Food 1 56
78 Men 1 74
79 Oral Care 1 74
80 Hair Care 1 74
81 Wellness 1 74
82 Tea and Coffees 1 90
83 Fruit Juice 1 68
84 Health Drinks 1 68
85 Laundries 1 67
86 Utensils 1 67
87 Cars 1 67
88 Floors and Glass 1 67
89 Kitchen 1 67
90 Bathroom 1 67
91 Shoes 1 67
92 Skin care 1 74
93 Baby Care 1 74
94 Beauty 1 74
95 Oral Care 1 74
96 Hair Care 1 74
97 Men 1 74
98 Wellness 1 74
99 Confectionary 1 68
104 Stationary 1 66
109 ABC 4 75
110 Plain Rice 1 62
111 Basmati Rice 1 62
112 Idli & Dosa Rice 1 62
113 Other Pulses 1 62
114 Soya Chunks 1 62
115 Beans 1 62
116 Millites 1 62
117 Atta 1 61
118 Suji And Rava 1 61
119 Besan And Maida 1 61
120 Rice Flour 1 61
121 Ragi Flour 1 61
122 Other Flour 1 61
123 Sunflower 1 60
124 Blended Oil 1 60
125 Other Oils 1 60
126 Olive 1 60
127 Ghee 1 60
128 Dates 1 54
129 Cashsew 1 54
130 Figs And Rasin 1 54
131 Almonds 1 54
132 Other Dry Fruits 1 54
133 Salt 1 55
134 Sugar 1 55
135 Jaggery 1 55
136 Sugar Free 1 55
137 Masala 1 1010
138 Ground Spices 1 1010
139 Wholes Sales 1 1010
140 Inter Spices 1 1010
141 Vermilli 1 56
142 Noodles 1 56
143 Pasta 1 56
144 Papad 1 56
145 Namkeen 1 56
146 Snacks 1 56
147 Chips 1 56
148 Pasta 1 56
149 Honey 1 57
150 Jam 1 57
151 Ketchup 1 57
152 Choco & Peanut 1 57
153 Sauce 1 57
154 Pickles 1 57
155 Vinegar 1 57
156 Soup Mix 1 59
157 Spreads 1 57
158 Breakfast Mix 1 59
159 Sweet MIx 1 59
160 Snacks Mix 1 59
161 Canned Food 1 59
162 Frozen Vegetables 1 59
163 Frozen Snacks 1 59
164 Gravy Mix 1 59
165 Soaps 1 69
product
表的样本数据如下
prod_id category_id name Bar Code
1336 54 3-Roses 8901725123123
1337 54 3-Roses-mind-sharp WFN201
1338 54 3-Roses-Natural-Care WFN208
1339 54 3-Roses-Top-Star WFN410
1340 54 Active-Wheel-Blue-Bar WFN403
1341 54 Active-Wheel-Gold WFN406
1342 54 Active-Wheel-Lemon-Orange WFN408
1343 54 Active-Wheel-with-Lemon- WFN472
1344 54 Annapurna-Crystal-Salt WFN409
1345 54 Annapurna-Farm-Fresh-Atta WFN404
1346 54 Annapurna-Powder-Salt WFN405
1347 54 Annapurna-Special-Atta WFN407
1348 54 AntiGerm-Vim WFN051
1349 55 AvianceXP 8901725121716
1351 54 Axe-Apollo WFN401
1352 54 Axe-Blast WFN100
1353 54 Axe-Click WFN125
1354 54 Axe-Dark-Temptation WFN124
1355 56 Axe-Dark-Temptation 8901242114307
1356 56 Axe-Dimension 901242114406
1357 54 BANANA FLOWER WFN075
1358 54 BANANA LEAF WFN077
1359 54 BANANA NENDRAN WFN003
1360 54 BANANA PLANT WFN607
1361 54 BANANA RAW WFN071
1362 54 BANANA RED WFN004
1363 54 BANANA ROBUSTA WFN001
1364 54 BANANA YELLAKI WFN002
1365 57 BASIL SOJI 500 GM 8901869197240
1366 54 BASLAE LEAF - 200 G WFN202
1367 54 BATANI SPROUT T00034
1368 54 BEANS AWAREYKAAYI WFN012
1370 54 BEANS COWPEA (Karamani) WFN010
1371 54 BEANS DOUBLE WFN013
1372 54 BEANS FRENCH FLAT WFN014
1373 54 BEANS FRENCH HARICOT- NATTI WFN006
1374 54 BEANS FRENCH ROUND WFN005