Why do I have to input ID (PK) - problem with auto generating ID












1














I'm creating renting a book method in my book rental. It looks like this:



@PostMapping("/book")
public void purchaseBook(@RequestParam("userID") int userID, @RequestParam("bookID") int bookID) {
bookRentalService.rentBook(userID, bookID);
}


After inputting userID and bookID, book if is available, it is added to BookRentalDB.



Relations between tables I made like this:
enter image description here



Entity of rentals:



package bookrental.model.book;

import bookrental.model.account.User;
import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class BookRentals {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Book book;
@OneToOne
private User user;
}


I've got problem with creating object of this entity(BookRentals) in Service. It needs to contain object of Book and object of User. I've created constructors for this object with only bookID for Book and userID for User. In prepareBookToRent Im asked to put ID of BookRentals class, too. In that case I can not to create object of it. Should it not be generated automatically? What should I do to make it work.



package bookrental.service.book;

import bookrental.model.account.User;
import bookrental.model.book.Book;
import bookrental.model.book.BookRentals;
import bookrental.repository.book.BookRepository;
import bookrental.repository.book.BookRentalsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookRentalService {

private final BookRepository bookRepository;
private final BookRentalsRepository bookRentalsRepository;

@Autowired
public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository) {
this.bookRepository = bookRepository;
this.bookRentalsRepository = bookRentalsRepository;
}

public void rentBook(int userID, int bookID) {
if (bookRepository.doesBookExistsWithGivenID(bookID)) {
Book bookToRent = bookRepository.findOne(bookID);
if (bookToRent.isAvailable()) {
updateBookAvailabilityAndSaveToDb(bookToRent);
BookRentals preparedBookToRent = prepareBookToRent(userID, bookID);
bookRentalsRepository.save(preparedBookToRent);
} else {
throw new IllegalArgumentException("Book is no available");
}
}
throw new IllegalArgumentException("Book does not exist!");
}


private BookRentals prepareBookToRent(int userID, int bookID) {
return new BookRentals(new Book(bookID),new User(userID)); // here im asked to input ID, too
}

private void updateBookAvailabilityAndSaveToDb(Book bookToRent) {
bookToRent.setAvailable(false);
bookRepository.save(bookToRent);
}
}


pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.book.rental.piotrek</groupId>
<artifactId>BookRental</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>

</project>


Staktrace



C:UsersAdminIdeaProjectsBookRentalsrcmainjavabookrentalservicebookBookRentalService.java
Error:(39, 16) java: no suitable constructor found for BookRentals(bookrental.model.book.Book,bookrental.model.account.User)
constructor bookrental.model.book.BookRentals.BookRentals(int,bookrental.model.book.Book,bookrental.model.account.User) is not applicable
(actual and formal argument lists differ in length)
constructor bookrental.model.book.BookRentals.BookRentals() is not applicable
(actual and formal argument lists differ in length)


As I said above, this is it:



enter image description here










share|improve this question
























  • Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
    – user10639668
    Nov 20 '18 at 9:29












  • @EugenCovaci updated pom.xml and stratrace.
    – pipilam
    Nov 20 '18 at 9:36










  • Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
    – user10639668
    Nov 20 '18 at 9:38












  • @EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
    – pipilam
    Nov 20 '18 at 9:39










  • @EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
    – Sarief
    Nov 20 '18 at 9:53
















1














I'm creating renting a book method in my book rental. It looks like this:



@PostMapping("/book")
public void purchaseBook(@RequestParam("userID") int userID, @RequestParam("bookID") int bookID) {
bookRentalService.rentBook(userID, bookID);
}


After inputting userID and bookID, book if is available, it is added to BookRentalDB.



Relations between tables I made like this:
enter image description here



Entity of rentals:



package bookrental.model.book;

import bookrental.model.account.User;
import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class BookRentals {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Book book;
@OneToOne
private User user;
}


I've got problem with creating object of this entity(BookRentals) in Service. It needs to contain object of Book and object of User. I've created constructors for this object with only bookID for Book and userID for User. In prepareBookToRent Im asked to put ID of BookRentals class, too. In that case I can not to create object of it. Should it not be generated automatically? What should I do to make it work.



package bookrental.service.book;

import bookrental.model.account.User;
import bookrental.model.book.Book;
import bookrental.model.book.BookRentals;
import bookrental.repository.book.BookRepository;
import bookrental.repository.book.BookRentalsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookRentalService {

private final BookRepository bookRepository;
private final BookRentalsRepository bookRentalsRepository;

@Autowired
public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository) {
this.bookRepository = bookRepository;
this.bookRentalsRepository = bookRentalsRepository;
}

public void rentBook(int userID, int bookID) {
if (bookRepository.doesBookExistsWithGivenID(bookID)) {
Book bookToRent = bookRepository.findOne(bookID);
if (bookToRent.isAvailable()) {
updateBookAvailabilityAndSaveToDb(bookToRent);
BookRentals preparedBookToRent = prepareBookToRent(userID, bookID);
bookRentalsRepository.save(preparedBookToRent);
} else {
throw new IllegalArgumentException("Book is no available");
}
}
throw new IllegalArgumentException("Book does not exist!");
}


private BookRentals prepareBookToRent(int userID, int bookID) {
return new BookRentals(new Book(bookID),new User(userID)); // here im asked to input ID, too
}

private void updateBookAvailabilityAndSaveToDb(Book bookToRent) {
bookToRent.setAvailable(false);
bookRepository.save(bookToRent);
}
}


pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.book.rental.piotrek</groupId>
<artifactId>BookRental</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>

</project>


Staktrace



C:UsersAdminIdeaProjectsBookRentalsrcmainjavabookrentalservicebookBookRentalService.java
Error:(39, 16) java: no suitable constructor found for BookRentals(bookrental.model.book.Book,bookrental.model.account.User)
constructor bookrental.model.book.BookRentals.BookRentals(int,bookrental.model.book.Book,bookrental.model.account.User) is not applicable
(actual and formal argument lists differ in length)
constructor bookrental.model.book.BookRentals.BookRentals() is not applicable
(actual and formal argument lists differ in length)


As I said above, this is it:



enter image description here










share|improve this question
























  • Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
    – user10639668
    Nov 20 '18 at 9:29












  • @EugenCovaci updated pom.xml and stratrace.
    – pipilam
    Nov 20 '18 at 9:36










  • Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
    – user10639668
    Nov 20 '18 at 9:38












  • @EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
    – pipilam
    Nov 20 '18 at 9:39










  • @EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
    – Sarief
    Nov 20 '18 at 9:53














1












1








1







I'm creating renting a book method in my book rental. It looks like this:



@PostMapping("/book")
public void purchaseBook(@RequestParam("userID") int userID, @RequestParam("bookID") int bookID) {
bookRentalService.rentBook(userID, bookID);
}


After inputting userID and bookID, book if is available, it is added to BookRentalDB.



Relations between tables I made like this:
enter image description here



Entity of rentals:



package bookrental.model.book;

import bookrental.model.account.User;
import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class BookRentals {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Book book;
@OneToOne
private User user;
}


I've got problem with creating object of this entity(BookRentals) in Service. It needs to contain object of Book and object of User. I've created constructors for this object with only bookID for Book and userID for User. In prepareBookToRent Im asked to put ID of BookRentals class, too. In that case I can not to create object of it. Should it not be generated automatically? What should I do to make it work.



package bookrental.service.book;

import bookrental.model.account.User;
import bookrental.model.book.Book;
import bookrental.model.book.BookRentals;
import bookrental.repository.book.BookRepository;
import bookrental.repository.book.BookRentalsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookRentalService {

private final BookRepository bookRepository;
private final BookRentalsRepository bookRentalsRepository;

@Autowired
public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository) {
this.bookRepository = bookRepository;
this.bookRentalsRepository = bookRentalsRepository;
}

public void rentBook(int userID, int bookID) {
if (bookRepository.doesBookExistsWithGivenID(bookID)) {
Book bookToRent = bookRepository.findOne(bookID);
if (bookToRent.isAvailable()) {
updateBookAvailabilityAndSaveToDb(bookToRent);
BookRentals preparedBookToRent = prepareBookToRent(userID, bookID);
bookRentalsRepository.save(preparedBookToRent);
} else {
throw new IllegalArgumentException("Book is no available");
}
}
throw new IllegalArgumentException("Book does not exist!");
}


private BookRentals prepareBookToRent(int userID, int bookID) {
return new BookRentals(new Book(bookID),new User(userID)); // here im asked to input ID, too
}

private void updateBookAvailabilityAndSaveToDb(Book bookToRent) {
bookToRent.setAvailable(false);
bookRepository.save(bookToRent);
}
}


pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.book.rental.piotrek</groupId>
<artifactId>BookRental</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>

</project>


Staktrace



C:UsersAdminIdeaProjectsBookRentalsrcmainjavabookrentalservicebookBookRentalService.java
Error:(39, 16) java: no suitable constructor found for BookRentals(bookrental.model.book.Book,bookrental.model.account.User)
constructor bookrental.model.book.BookRentals.BookRentals(int,bookrental.model.book.Book,bookrental.model.account.User) is not applicable
(actual and formal argument lists differ in length)
constructor bookrental.model.book.BookRentals.BookRentals() is not applicable
(actual and formal argument lists differ in length)


As I said above, this is it:



enter image description here










share|improve this question















I'm creating renting a book method in my book rental. It looks like this:



@PostMapping("/book")
public void purchaseBook(@RequestParam("userID") int userID, @RequestParam("bookID") int bookID) {
bookRentalService.rentBook(userID, bookID);
}


After inputting userID and bookID, book if is available, it is added to BookRentalDB.



Relations between tables I made like this:
enter image description here



Entity of rentals:



package bookrental.model.book;

import bookrental.model.account.User;
import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class BookRentals {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Book book;
@OneToOne
private User user;
}


I've got problem with creating object of this entity(BookRentals) in Service. It needs to contain object of Book and object of User. I've created constructors for this object with only bookID for Book and userID for User. In prepareBookToRent Im asked to put ID of BookRentals class, too. In that case I can not to create object of it. Should it not be generated automatically? What should I do to make it work.



package bookrental.service.book;

import bookrental.model.account.User;
import bookrental.model.book.Book;
import bookrental.model.book.BookRentals;
import bookrental.repository.book.BookRepository;
import bookrental.repository.book.BookRentalsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookRentalService {

private final BookRepository bookRepository;
private final BookRentalsRepository bookRentalsRepository;

@Autowired
public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository) {
this.bookRepository = bookRepository;
this.bookRentalsRepository = bookRentalsRepository;
}

public void rentBook(int userID, int bookID) {
if (bookRepository.doesBookExistsWithGivenID(bookID)) {
Book bookToRent = bookRepository.findOne(bookID);
if (bookToRent.isAvailable()) {
updateBookAvailabilityAndSaveToDb(bookToRent);
BookRentals preparedBookToRent = prepareBookToRent(userID, bookID);
bookRentalsRepository.save(preparedBookToRent);
} else {
throw new IllegalArgumentException("Book is no available");
}
}
throw new IllegalArgumentException("Book does not exist!");
}


private BookRentals prepareBookToRent(int userID, int bookID) {
return new BookRentals(new Book(bookID),new User(userID)); // here im asked to input ID, too
}

private void updateBookAvailabilityAndSaveToDb(Book bookToRent) {
bookToRent.setAvailable(false);
bookRepository.save(bookToRent);
}
}


pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.book.rental.piotrek</groupId>
<artifactId>BookRental</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>



<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>

</project>


Staktrace



C:UsersAdminIdeaProjectsBookRentalsrcmainjavabookrentalservicebookBookRentalService.java
Error:(39, 16) java: no suitable constructor found for BookRentals(bookrental.model.book.Book,bookrental.model.account.User)
constructor bookrental.model.book.BookRentals.BookRentals(int,bookrental.model.book.Book,bookrental.model.account.User) is not applicable
(actual and formal argument lists differ in length)
constructor bookrental.model.book.BookRentals.BookRentals() is not applicable
(actual and formal argument lists differ in length)


As I said above, this is it:



enter image description here







java spring-boot primary-key crud






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:35

























asked Nov 20 '18 at 9:19









pipilam

795




795












  • Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
    – user10639668
    Nov 20 '18 at 9:29












  • @EugenCovaci updated pom.xml and stratrace.
    – pipilam
    Nov 20 '18 at 9:36










  • Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
    – user10639668
    Nov 20 '18 at 9:38












  • @EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
    – pipilam
    Nov 20 '18 at 9:39










  • @EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
    – Sarief
    Nov 20 '18 at 9:53


















  • Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
    – user10639668
    Nov 20 '18 at 9:29












  • @EugenCovaci updated pom.xml and stratrace.
    – pipilam
    Nov 20 '18 at 9:36










  • Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
    – user10639668
    Nov 20 '18 at 9:38












  • @EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
    – pipilam
    Nov 20 '18 at 9:39










  • @EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
    – Sarief
    Nov 20 '18 at 9:53
















Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
– user10639668
Nov 20 '18 at 9:29






Please post the error stacktrace. Also add info about the database type and version, Spring boot version, Java version, JPA provider.
– user10639668
Nov 20 '18 at 9:29














@EugenCovaci updated pom.xml and stratrace.
– pipilam
Nov 20 '18 at 9:36




@EugenCovaci updated pom.xml and stratrace.
– pipilam
Nov 20 '18 at 9:36












Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
– user10639668
Nov 20 '18 at 9:38






Your issue has nothing to do with JPA auto generated id, but with improper use of Lombok.
– user10639668
Nov 20 '18 at 9:38














@EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
– pipilam
Nov 20 '18 at 9:39




@EugenCovaci should I exclude then ID from costructor as Sarief in asnswer said?
– pipilam
Nov 20 '18 at 9:39












@EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
– Sarief
Nov 20 '18 at 9:53




@EugenCovaci see updated answer then. Didn't notice OneToOne annotation on the mapping class. It didn't make sense, so my brain ignored it
– Sarief
Nov 20 '18 at 9:53












1 Answer
1






active

oldest

votes


















1














You're wrong, you created all args contstructor. You have 3 attributes. This means, you have all args constructor with 3 arguments not 2.



Just create custom constructor.



public BookRentals (Book book, User user) {
// logic
}


ps: why not possible to exclude some parameter from all args: Java Lombok: Omitting one field in @AllArgsConstructor?



upd: comments are stating something crazy. So, if you got one to one, use annotation on entity directly. If you got many to many, consider OneToMany, as having multiple users renting same book is not realistic.



refer to this for table design: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?






share|improve this answer























  • And ID will be genereated automatically?
    – pipilam
    Nov 20 '18 at 9:35










  • Id, if annotated properly, will be generated once you save the entity
    – Sarief
    Nov 20 '18 at 9:37






  • 1




    I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
    – pipilam
    Nov 20 '18 at 9:40






  • 1




    @pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
    – Sarief
    Nov 20 '18 at 12:14






  • 1




    well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
    – pipilam
    Nov 20 '18 at 14:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389763%2fwhy-do-i-have-to-input-id-pk-problem-with-auto-generating-id%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You're wrong, you created all args contstructor. You have 3 attributes. This means, you have all args constructor with 3 arguments not 2.



Just create custom constructor.



public BookRentals (Book book, User user) {
// logic
}


ps: why not possible to exclude some parameter from all args: Java Lombok: Omitting one field in @AllArgsConstructor?



upd: comments are stating something crazy. So, if you got one to one, use annotation on entity directly. If you got many to many, consider OneToMany, as having multiple users renting same book is not realistic.



refer to this for table design: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?






share|improve this answer























  • And ID will be genereated automatically?
    – pipilam
    Nov 20 '18 at 9:35










  • Id, if annotated properly, will be generated once you save the entity
    – Sarief
    Nov 20 '18 at 9:37






  • 1




    I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
    – pipilam
    Nov 20 '18 at 9:40






  • 1




    @pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
    – Sarief
    Nov 20 '18 at 12:14






  • 1




    well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
    – pipilam
    Nov 20 '18 at 14:34
















1














You're wrong, you created all args contstructor. You have 3 attributes. This means, you have all args constructor with 3 arguments not 2.



Just create custom constructor.



public BookRentals (Book book, User user) {
// logic
}


ps: why not possible to exclude some parameter from all args: Java Lombok: Omitting one field in @AllArgsConstructor?



upd: comments are stating something crazy. So, if you got one to one, use annotation on entity directly. If you got many to many, consider OneToMany, as having multiple users renting same book is not realistic.



refer to this for table design: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?






share|improve this answer























  • And ID will be genereated automatically?
    – pipilam
    Nov 20 '18 at 9:35










  • Id, if annotated properly, will be generated once you save the entity
    – Sarief
    Nov 20 '18 at 9:37






  • 1




    I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
    – pipilam
    Nov 20 '18 at 9:40






  • 1




    @pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
    – Sarief
    Nov 20 '18 at 12:14






  • 1




    well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
    – pipilam
    Nov 20 '18 at 14:34














1












1








1






You're wrong, you created all args contstructor. You have 3 attributes. This means, you have all args constructor with 3 arguments not 2.



Just create custom constructor.



public BookRentals (Book book, User user) {
// logic
}


ps: why not possible to exclude some parameter from all args: Java Lombok: Omitting one field in @AllArgsConstructor?



upd: comments are stating something crazy. So, if you got one to one, use annotation on entity directly. If you got many to many, consider OneToMany, as having multiple users renting same book is not realistic.



refer to this for table design: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?






share|improve this answer














You're wrong, you created all args contstructor. You have 3 attributes. This means, you have all args constructor with 3 arguments not 2.



Just create custom constructor.



public BookRentals (Book book, User user) {
// logic
}


ps: why not possible to exclude some parameter from all args: Java Lombok: Omitting one field in @AllArgsConstructor?



upd: comments are stating something crazy. So, if you got one to one, use annotation on entity directly. If you got many to many, consider OneToMany, as having multiple users renting same book is not realistic.



refer to this for table design: How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 12:03

























answered Nov 20 '18 at 9:34









Sarief

387317




387317












  • And ID will be genereated automatically?
    – pipilam
    Nov 20 '18 at 9:35










  • Id, if annotated properly, will be generated once you save the entity
    – Sarief
    Nov 20 '18 at 9:37






  • 1




    I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
    – pipilam
    Nov 20 '18 at 9:40






  • 1




    @pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
    – Sarief
    Nov 20 '18 at 12:14






  • 1




    well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
    – pipilam
    Nov 20 '18 at 14:34


















  • And ID will be genereated automatically?
    – pipilam
    Nov 20 '18 at 9:35










  • Id, if annotated properly, will be generated once you save the entity
    – Sarief
    Nov 20 '18 at 9:37






  • 1




    I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
    – pipilam
    Nov 20 '18 at 9:40






  • 1




    @pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
    – Sarief
    Nov 20 '18 at 12:14






  • 1




    well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
    – pipilam
    Nov 20 '18 at 14:34
















And ID will be genereated automatically?
– pipilam
Nov 20 '18 at 9:35




And ID will be genereated automatically?
– pipilam
Nov 20 '18 at 9:35












Id, if annotated properly, will be generated once you save the entity
– Sarief
Nov 20 '18 at 9:37




Id, if annotated properly, will be generated once you save the entity
– Sarief
Nov 20 '18 at 9:37




1




1




I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
– pipilam
Nov 20 '18 at 9:40




I've got it properly annotated, right? :D Why should I consider @OneToMany - one book can be only once rented by only one user.
– pipilam
Nov 20 '18 at 9:40




1




1




@pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
– Sarief
Nov 20 '18 at 12:14




@pipilam as for why... this is convention. This conventions are used not only by you but by frameworks you use. Doing desing in accepted conventions helps you to easily integrate with those frameworks. QueryDsl and Spring Data can generate meta entities based on those designs, other developers immidiately know how things work, etc.
– Sarief
Nov 20 '18 at 12:14




1




1




well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
– pipilam
Nov 20 '18 at 14:34




well, I tried to understand concept of what is in the link. I think that you didn't understand how I'm trying to make it. I illustrated it on the picture. Im creating 3 entities - one for books, one for users and one for rentals. I need 3 tables to it. I need OneToOne relation. Unfortunately I do not understand what you mean. I think I will leave like I have written.
– pipilam
Nov 20 '18 at 14:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389763%2fwhy-do-i-have-to-input-id-pk-problem-with-auto-generating-id%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]