RideController.java
@RestController
public class RideController {
@Autowired
RideService rideService;
@PostMapping(path = "/api/ride")
public ResponseEntity<Ride> createNewRide(@RequestBody Ride ride) {
return ResponseEntity.ok(rideService.save(ride));
}
}
RideService.java
public interface RideService {
public Ride save(Ride ride);
public Ride findById(Long rideId);
public List<Ride> getAll();
}
RideServiceImpl.java
@Service
public class RideServiceImpl implements RideService {
@Autowired
RideRepository rideRepository;
public Ride save(Ride ride) {
return rideRepository.save(ride);
}
public Ride findById(Long rideId) {
Optional<Ride> optionalRide = rideRepository.findById(rideId);
if (optionalRide.isPresent()) {
return optionalRide.get();
} else
return null;
}
@Override
public List<Ride> getAll() {
List<Ride> rideList = new ArrayList<>();
rideRepository.findAll().forEach(rideList::add);
return rideList;
}
}
RideRepository.java
@RestResource(exported = false)
public interface RideRepository extends CrudRepository<Ride, Long> {
}
PersonRepository.java
@RestResource(exported=false)
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
Optional<Person> findById(Long id);
}
Person.java
@Entity
@Table(name = "person")
@SecondaryTable(name = "ride")
public class Person implements Serializable{
private static final long serialVersionUID = 7401548380514451401L;
public Person() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column(name = "name")
String name;
@NotNull
@Email
@Column(name = "email")
String email;
@Column(name = "registration_number")
String registrationNumber;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((registrationNumber == null) ? 0 : registrationNumber.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (registrationNumber == null) {
if (other.registrationNumber != null)
return false;
} else if (!registrationNumber.equals(other.registrationNumber))
return false;
return true;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", email=" + email + ", registrationNumber=" + registrationNumber + "]";
}
}
Ride.java
@Entity
@Table(name = "ride")
public class Ride implements Serializable{
private static final long serialVersionUID = 9097639215351514001L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@NotNull
@Column(name = "start_time")
String startTime;
@NotNull
@Column(name = "end_time")
String endTime;
@Column(name = "distance")
Long distance;
@OneToOne
@JoinColumn(name = "driver_id")
Person driver;
@OneToOne
@JoinColumn(name = "rider_id")
Person rider;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public Long getDistance() {
return distance;
}
public void setDistance(Long distance) {
this.distance = distance;
}
public Person getDriver() {
return driver;
}
public void setDriver(Person driver) {
this.driver = driver;
}
public Person getRider() {
return rider;
}
public void setRider(Person rider) {
this.rider = rider;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((distance == null) ? 0 : distance.hashCode());
result = prime * result + ((driver == null) ? 0 : driver.hashCode());
result = prime * result + ((endTime == null) ? 0 : endTime.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((rider == null) ? 0 : rider.hashCode());
result = prime * result + ((startTime == null) ? 0 : startTime.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ride other = (Ride) obj;
if (distance == null) {
if (other.distance != null)
return false;
} else if (!distance.equals(other.distance))
return false;
if (driver == null) {
if (other.driver != null)
return false;
} else if (!driver.equals(other.driver))
return false;
if (endTime == null) {
if (other.endTime != null)
return false;
} else if (!endTime.equals(other.endTime))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (rider == null) {
if (other.rider != null)
return false;
} else if (!rider.equals(other.rider))
return false;
if (startTime == null) {
if (other.startTime != null)
return false;
} else if (!startTime.equals(other.startTime))
return false;
return true;
}
@Override
public String toString() {
return "Ride [id=" + id + ", startTime=" + startTime + ", endTime=" + endTime + ", distance=" + distance + ", driver=" + driver + ", rider=" + rider + "]";
}
}
db.changelog-1.0.xml
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="crossover" id="1001122001_create_tables">
<createTable tableName="person">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints primaryKey="true" primaryKeyName="pk_person"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="email" type="VARCHAR(255)"/>
<column name="registration_number" type="VARCHAR(255)"/>
</createTable>
<createTable tableName="ride">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints primaryKey="true" primaryKeyName="pk_ride"/>
</column>
<column name="driver_id" type="BIGINT"/>
<column name="rider_id" type="BIGINT"/>
<column name="start_time" type="VARCHAR(255)"/>
<column name="end_time" type="VARCHAR(255)"/>
<column name="distance" type="BIGINT"/>
</createTable>
<addForeignKeyConstraint baseColumnNames="driver_id"
baseTableName="ride"
constraintName="fk_address_person_driver"
onDelete="CASCADE"
onUpdate="RESTRICT"
referencedColumnNames="id"
referencedTableName="person"/>
<addForeignKeyConstraint baseColumnNames="rider_id"
baseTableName="ride"
constraintName="fk_address_person_rider"
onDelete="CASCADE"
onUpdate="RESTRICT"
referencedColumnNames="id"
referencedTableName="person"/>
</changeSet>
</databaseChangeLog>
db.changelog-master.xml
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<include file="db/changelog/db.changelog-1.0.xml"/>
</databaseChangeLog>
PersonService.java
public interface PersonService {
public List<Person> getAll();
public Person save(Person p);
public Person findById(Long personId);
}
PersonServiceImpl.java
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
PersonRepository personRepository;
@Override
public List<Person> getAll() {
List<Person> personList = new ArrayList<>();
personRepository.findAll().forEach(personList::add);
return personList;
}
public Person save(Person p) {
return personRepository.save(p);
}
@Override
public Person findById(Long personId) {
Optional<Person> dbPerson = personRepository.findById(personId);
return dbPerson.orElse(null);
}
}
У меня есть следующий проект ..не сделано или написано мной, и я пытаюсь решить ошибку с этим проектом.В основном, когда я пытаюсь создать поездку "/ api / ride", используя приведенный ниже общий запрос, я всегда получаю неправильные данные, сохраняемые в базу данных.
Запрос:
{
"distance": 20,
"driver": {
"email": "testdriver@test.com",
"id": 1,
"name": "test driver",
"registrationNumber": "7895612313"
},
"endTime": "2018-10-07T18:46:19",
"id": 100,
"rider": {
"email": "csfragstatest@test.com",
"id": 1,
"name": "csfragstatest",
"registrationNumber": "54564897456"
},
"startTime": "2018-10-06T18:46:19"
}
Ответ:
{
"id": 1,
"startTime": "2018-10-06T18:46:19",
"endTime": "2018-10-07T18:46:19",
"distance": 20,
"driver": {
"id": 1,
"name": "csfragstatest",
"email": "csfragstatest@test.com",
"registrationNumber": "465123167"
},
"rider": {
"id": 1,
"name": "csfragstatest",
"email": "csfragstatest@test.com",
"registrationNumber": "465123167"
}
}
Пожалуйста, помогите мне, если я ошибаюсь здесь .... У меня нет большой идеи, используя пружинуjpa и hibernate ... Я так растерялся, ломая голову против этого .. Любая помощь приветствуется ... Заранее спасибо!