Skip to content

arvindsis11/springJPAdemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Spring Boot ManyToOne Unidirectional Mapping(Student --> College) Using Annotations

  1. In Many-To-One Unidirectional mapping, one table has a foreign key column that references the primary key of associated table. By Unidirectional relationship means only one side navigation is possible (STUDENT to COLLEGE in this example).

  2. Many students can enroll at one College. And one College can have many students.

Student.java,Faculty.java,Employee.java

@ManyToOne(optional = false,cascade = CascadeType.ALL)
@JoinColumn(name = "c_id")#college_id
private University university;

Explanation:

  1. @ManyToOne indicates that Many student tuples can refer to one College tuple. Also note that we have provided optional=false means this relationship becomes mandatory , no student row can be saved without a College tuple reference.

@JoinColumn says that there is a column c_id(i.e. college id) in Student table which will refer(foreign key) to primary key of the College table. In this example only Student to College entity navigation is possible. Not viceversa. In practice, however, you are free to use query language to find all the student for a given College.
cascade = CascadeType.ALL indicates that, once we start any operation(CURD) on student table , it will reflects to College table.
Please refer to PostManSpecs.txt for more info.

other specifications:

  1. java version : 11
  2. database : h2 database

CRUD OPERATIONS:

  1. CREATE Operation: It performs the INSERT statement to create a new record.
  2. READ Operation: It reads table records based on the input parameter.
  3. UPDATE Operation: It executes an update statement on the table. It is based on the input parameter.
  4. DELETE Operation: It deletes a specified row in the table. It is also based on the input parameter.

SWAGGER CONFIGURATION:

  1. create Docket:
 @Bean
    public Docket swaggerConfiguration() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.any()).build()
                .apiInfo(metaData());

    }
  1. Add this dependencies to pom.xml:
<dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>2.6.1</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.6.1</version>
         <scope>compile</scope>
      </dependency>

note: use exact version. in case of any error add this line to application.properties:

spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

error might occure if you use swagger v3 or above because it doesn't supports matcher

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL 

\ [jar:file:/C:/Users/user/.m2/repository/io/springfox/springfox-spring-web/3.0.0/springfox-spring-web-

3.0.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1;\ nested exception is

About

jpa relationship mapping -->one-to-many many-to-one using spring boot annotations + ALL Basic crud operations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages