|
62 | 62 | <td><samp>load()</samp> should be used if you are sure that instance exists.</td>
|
63 | 63 | </tr>
|
64 | 64 | </table>
|
65 | | - |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 65 |
|
70 | 66 | 9. __Different Hibernate Inheritence Strategies__<br>
|
71 | 67 | - Table Per Hierarchy
|
|
172 | 168 | - `@JoinTable`
|
173 | 169 | - `@JoinColumn`
|
174 | 170 |
|
| 171 | +24. __Properties in `HBM XML` file__<br> |
| 172 | + - The mapping document is an XML document having `<hibernate-mapping>` as the root element, which contains all the `<class>` elements. |
| 173 | + |
| 174 | + - The `<class>` elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute. |
| 175 | + |
| 176 | + - The `<meta>` element is optional element and can be used to create the class description. |
| 177 | + |
| 178 | + - The `<id>` element maps the unique ID attribute in class to the primary key of the database table. The name attribute of the id element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. |
| 179 | + |
| 180 | + - The `<generator>` element within the id element is used to generate the primary key values automatically. The class attribute of the generator element is set to native to let hibernate pick up either identity, sequence, or hilo algorithm to create primary key depending upon the capabilities of the underlying database. |
| 181 | + |
| 182 | + - The `<property>` element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. |
| 183 | + |
| 184 | +25. __Named Query & Criteria Query__<br> |
| 185 | + - Named Query: |
| 186 | + > A named query is a statically defined query with a predefined unchangeable query string. They're validated when the session factory is created, thus making the application to fail fast in case of an error. |
| 187 | + - Criteria Query: |
| 188 | + > The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc. |
| 189 | + |
175 | 190 |
|
176 | | -<hr> |
177 | 191 |
|
178 | 192 |
|
| 193 | + |
| 194 | +<hr> |
| 195 | + |
| 196 | + |
179 | 197 | ## Spring FAQs
|
180 | 198 |
|
181 | 199 | 1. __BeanFactory v/s ApplicationContext__<br>
|
|
248 | 266 | - This annotation maps HTTP requests to handler methods of MVC and REST controllers.
|
249 | 267 | - The `@RequestMapping` annotation can be applied to class-level and/or method-level in a controller.
|
250 | 268 |
|
251 | | -6. __`@Path` Variable__<br> |
252 | | - It is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose @PathVariable annotation is used in Spring mvc framework. |
| 269 | +6. __`@PathVariable` & `@RequestParam`__<br> |
| 270 | + - `@PathVariable` |
| 271 | + > It is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose `@PathVariable` annotation is used in Spring mvc framework. |
| 272 | + - `@RequestParam` |
| 273 | + > In Spring MVC, the `@RequestParam` annotation is used to read the form data and bind it automatically to the parameter present in the provided method. |
253 | 274 |
|
254 | 275 | 7. __Declarative Transaction Management in Spring__<br>
|
255 | 276 | Declarative transaction management approach allows you to manage the transaction with the help of configuration instead of hard coding in your source code. This means that you can separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions. The bean configuration will specify the methods to be transactional.
|
|
275 | 296 | - `@Service`
|
276 | 297 | - `@Repository`
|
277 | 298 |
|
278 | | -9. __Role of Controller in Spring__<br> |
| 299 | +9. __Role of Controller in Spring__<br> |
279 | 300 | - A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
|
280 | 301 | - A front controller is defined as a controller that handles all requests for a Web Application. DispatcherServlet servlet is the front controller in Spring MVC that intercepts every request and then dispatches requests to an appropriate controller.
|
281 | 302 |
|
|
313 | 334 | - Spring AOP Module
|
314 | 335 | - Spring WEB-MVC Module
|
315 | 336 |
|
316 | | -16. |
317 | | - |
318 | | - |
319 | | - |
320 | | - |
321 | | - |
| 337 | +16. __Autowiring Collection Types__<br> |
| 338 | + 1. *autowire byName* – For this type of autowiring, setter method is used for dependency injection. Also the variable name should be same in the class where we will inject the dependency and in the spring bean configuration file. |
| 339 | + 2. *autowire byType* – For this type of autowiring, class type is used. So there should be only one bean configured for this type in the spring bean configuration file. |
| 340 | + 3. *autowire by constructor* – This is almost similar to autowire byType, the only difference is that constructor is used to inject the dependency. |
| 341 | + 4. *autowire by autodetect* – If you are on Spring 3.0 or older versions, this is one of the autowire options available. This option was used for autowire by constructor or byType, as determined by Spring container. |
| 342 | + 5. *`@Autowired` annotation* – We can use Spring `@Autowired` annotation for spring bean autowiring. `@Autowired` annotation can be applied on variables and methods for autowiring byType. We can also use `@Autowired` annotation on constructor for constructor based spring autowiring. |
| 343 | + 6. *`@Qualifier`annotation* – This annotation is used to avoid conflicts in bean mapping and we need to provide the bean name that will be used for autowiring. This way we can avoid issues where multiple beans are defined for same type. This annotation usually works with the `@Autowired` annotation. For constructors with multiple arguments, we can use this annotation with the argument names in the method. |
| 344 | + |
| 345 | +17. __What is `ModelAndView`?__<br> |
| 346 | + `ModelAndView` is an object that holds both the model and view. The handler returns the `ModelAndView` object and `DispatcherServlet` resolves the view using View Resolvers and View. The View is an object which contains view name in the form of the String and model is a map to add multiple objects. |
| 347 | + |
| 348 | +18. __View Resolvers__<br> |
| 349 | + - Spring MVC Framework provides the ViewResolver interface, that maps view names to actual views. |
| 350 | + - It also provides the View interface, which addresses the request of a view to the view technology. So when a ModelAndView instance is returned by a Controller, the view resolver will resolve the view according to the view name. |
| 351 | + - *Types*: |
| 352 | + - `AbstractCachingViewResolver` |
| 353 | + - `XmlViewResolver` |
| 354 | + - `ResourceBundleViewResolver` |
| 355 | + - `UrlBasedViewResolver` |
| 356 | + - `InternalResourceViewResolver` |
| 357 | + - `ContentNegotiatingViewResolver` |
| 358 | + |
| 359 | +19. __`@ModelAttribute` Annotation__<br> |
| 360 | + - Firstly, it can be used to inject data objects the model before a JSP loads. This makes it particularly useful in ensuring that a JSP has all the data is needs to display itself. The injection is achieved by binding a method return value to the model. |
| 361 | + - Secondly, it can be used to read data from an existing model assigning it to handler method parameters. |
| 362 | + |
| 363 | +20. __Locating `hibernate.cfg.xml` file__<br> |
| 364 | + To ask Hibernate look for your `hibernate.cfg.xml` file in other directory, you can modify the default Hibernate’s SessionFactory class by passing your `hibernate.cfg.xml` file path as an argument into the `configure()` method: |
| 365 | + ```java |
| 366 | + SessionFactory sessionFactory = new Configuration() |
| 367 | + .configure("/com/mkyong/persistence/hibernate.cfg.xml") |
| 368 | + .buildSessionFactory(); |
| 369 | + |
| 370 | + return sessionFactory; |
| 371 | + ``` |
322 | 372 |
|
323 | 373 |
|
324 | 374 |
|
|
0 commit comments