Spring-1
Spring-1
Spring-1
java
package com.example.demo.model;
import javax.persistence.*;
import java.util.List;
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
// Employee.java
package com.example.demo.model;
import javax.persistence.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
// DepartmentRepository.java
package com.example.demo.repository;
import com.example.demo.model.Department;
import org.springframework.data.jpa.repository.JpaRepository;
// EmployeeRepository.java
package com.example.demo.repository;
import com.example.demo.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Department;
import com.example.demo.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/departments")
public class DepartmentController {
@Autowired
private DepartmentRepository departmentRepository;
@GetMapping
public List<Department> getAllDepartments() {
return departmentRepository.findAll();
}
@PostMapping
public Department createDepartment(@RequestBody Department department) {
return departmentRepository.save(department);
}
}
// EmployeeController.java
package com.example.demo.controller;
import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
@PostMapping
public Employee createEmployee(@RequestBody Employee employee) {
return employeeRepository.save(employee);
}
}
# application.properties
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
// App.js
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, FlatList } from 'react-native';
import axios from 'axios';
useEffect(() => {
fetchDepartments();
fetchEmployees();
}, []);
return (
<View style={{ padding: 20 }}>
<Text>Departments</Text>
<FlatList
data={departments}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<Text>{item.name}</Text>
)}
/>
<TextInput
placeholder="Department Name"
value={departmentName}
onChangeText={setDepartmentName}
/>
<Button title="Add Department" onPress={addDepartment} />
<Text>Employees</Text>
<FlatList
data={employees}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => (
<Text>{item.name} - {item.department.name}</Text>
)}
/>
<TextInput
placeholder="Employee Name"
value={employeeName}
onChangeText={setEmployeeName}
/>
<TextInput
placeholder="Select Department ID"
value={selectedDepartment}
onChangeText={setSelectedDepartment}
/>
<Button title="Add Employee" onPress={addEmployee} />
</View>
);
};