• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

How would you immediately get the ID primary key generated from a POST request in Javascript?

 
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm utilizing Java Spring Boot with Javascript.   This part of the application the school registers their school for the prom ticket application, and then they login with their ID number to start entering data and using the web application.  


What I want it to do after it posts, is to get the ID from MySQL of the entry that it had just entered, and a window box saying, "Successfully Registered!  Your school ID number is #"  the pound sign being the number.

However, if I just do a Get request, it may get all of them, and if I just search by the school name after the get all and then give back that number, what if two schools registered the same school name?  

Here is my function.



What do I do next to get that same entry and get the primary key of the school ID from it?  I put in null, which give it an auto increment because it is not null in MySQL.  

Would I do a get request, and if I did, how would I filter it to get just the request just sent without having to query by school name?   Is this even possible?  



I work for a hotel chain, and after we register a membership for someone, it gives back a message saying their membership number.  I want to do something like that.  
 
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should edit the server-side of the application to return the ID with the rest of the response after it has created the new school.
 
Nathan Milota
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You should edit the server-side of the application to return the ID with the rest of the response after it has created the new school.



It is done through a rest controller and it's a POST method, so I'm not sure how to do that.  Is there an example somewhere?  

The MySQL has a function to get the last ID.  

I'm on my phone now, so I unfortunately don't have a copy of the post method on me.
 
Nathan Milota
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my post method which is normally a void method, I changed it to return an int, and when I returned the school's id, it gave a value of 0 in Java, so when it posts to the database, by the time I retrieve the ID of it, it is still 0 for some reason.
 
Nathan Milota
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the @GeneratedValue(strategy = GenerationType.IDENTITY) and got the right value.  I'm still tackling how to send it over to the javascript function now.
 
Stephan van Hulst
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just return an object containing the ID and other info related to the registered school from your REST controller action:
 
Nathan Milota
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the well prepared response.

I'm still new to Spring Boot, so a lot of it goes over my head right now.  

What I did was I changed the post method to an int instead of a void, and then returned the id of the school and put a pop up window saying what their school number is after they registered based on what the method returns.  


I'm curious though.  Why so many classes?  Just for cleaner code or encapsulation purposes?  

The tutorials and Spring Boot projects with controllers I've seen didn't have so many classes.  Just one Controller, Service, and Repository interface.  That along with the class of the type object itself as well.
 
Stephan van Hulst
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From experience I can tell you that requests and responses often need to change to include more data than what you thought they needed in the first iteration of your application. Response wrappers decouple the response from the domain model, and leave you free to add more properties in the future without breaking your API.

Your JSON is also nicer to read. What is a more informative response after a registration:Or:
Note that the second example would require registerSchool() to return a RegisterSchoolResponse instead of a GetSchoolResponse.
 
Nathan Milota
Ranch Hand
Posts: 351
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the problem I'm having with Spring Boot is that I cannot understand what Spring really is to begin with.  

Every tutorial, book, video, website that explains it has something different from a bean, xml files, configurations, etc.  None of it seems consistent.  
 
Stephan van Hulst
Saloon Keeper
Posts: 11472
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because Spring is lots of things. It is a set of tools that solve common problems in web application development.

Imagine we didn't have Servlets. Every time you wanted to write a web service, you'd first have to write a web server that listens to an internet socket and parses and formats HTTP messages. You'd quickly get tired of this and write a library that you can use in all your web services.

Thankfully you don't have to, because Java EE contains the Servlet specification that says if you extend HttpServlet and package your application as a WAR, a compliant web application container will do the HTTP handling for you, and you just need to handle the HttpServletRequest.

As it turns out, even when servlets handle the details of HTTP for you, there are still a lot of repetitive task that you need to perform in every web application:

  • Load the application configuration
  • Route the request to the code that will handle it
  • Convert data in the request to objects from your model
  • Find out who the user is
  • Make sure the user is allowed to make the request
  • Initialize services that you need to fulfill the request
  • Create a view from the response data using a template

  • These tasks, and others I haven't mentioned, would drive you completely nuts if you had to write code to do them for every web application you wrote.

    Spring handles all this for you, so you can focus on what makes your web application different from other web applications. Because there is so much plumming to do, Spring is very large and complex, and beginners will often feel very overwhelmed. Thankfully, their user guide is very well written and with some practice you will be able to perform complex tasks very easily.

    I would say that the most important problem that Spring solves is dependency injection. It allows you to tell Spring what beans (services) your application will need, and how they are configured. Classes that need the service will have them injected with the @Autowired annotation.

    Note that Spring is not your only choice. Besides the Servlet specification, Java EE contains specifications for other components that handle the repetitive tasks that Spring also handles.

    For instance, instead of Spring REST Controllers, you can use JAX-RS. Instead of Spring dependency injection, you can use CDI. Instead of Spring Security, you can use Java EE Security API. And when Java EE 10 comes, you can probably use the MVC specification instead of Spring MVC. Personally I prefer the Java EE specs over Spring, but at this point it's mostly just a matter of what you're familiar with.
     
    Nathan Milota
    Ranch Hand
    Posts: 351
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So, if the repository is an interface, why do we never implement it?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11472
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What do you mean?
     
    Nathan Milota
    Ranch Hand
    Posts: 351
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:What do you mean?



    You only instantiated the repository, but didn't implement it.  Why do we make the interface?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11472
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where did I instantiate it? And who says we don't implement the interface?
     
    Bartender
    Posts: 21731
    148
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Nathan Milota wrote:Part of the problem I'm having with Spring Boot is that I cannot understand what Spring really is to begin with.  

    Every tutorial, book, video, website that explains it has something different from a bean, xml files, configurations, etc.  None of it seems consistent.  



    Spring is a framework designed to construct and serve up JavaBeans. At its heart is a factory object (the BeanFactory) which is a resource that can be invoked to return a named JavaBean. Spring will create and initialize the bean if it does not exist, and that includes any secondary beans that are referenced by that bean's definition. Everything else is built on that foundation. Formally, Spring provides what is known as an Inversion of Control (IoC) system. IoC is about taking things defining their relationships so that an external mechanism (for example, Spring) can wire them together, rather like TinkerToys™. This can be contrasted with the Service Locator Design Pattern, where the beans have to find other beans themselves, and thus have to have locator code hard-coded into each bean that needs to connect. IoC can thus be more easily used with POJOs.

    The beans are defined via a master definition XML file and/or by placing annotations on the class definitions for the JavaBeans that you wish for Spring to manage. You can actually build very complex definitions using many different sources and data formats, but the XML and annotations are sufficient for most purposes.

    This mechanism is at the core of Spring, but successive layers have been built around it to provide convenience and standardization. For example, Spring Data provides data persistence services for a wide variety of storage mechanisms, including raw JDBC, JPA, noSQL storage such as MongoDB and graph databases like Neo4J. Spring Data provides interface consistency as well as automatically doing the "grunt" work like freeing up connections when a database exception occurs, doing automatic rollback on failure, and so forth.

    There are quite a few of these extensions. Spring Boot is the one that builds single-purpose web applications with built-in dedicated web server. Such constructs make it easy to create portable (OS-independent) GUI applications that can be deployed as a simple, single unit (as opposed to the messier traditional way that requires separate installation of a webapp server and configuration and deployment of the application itself).

    Multiple Spring modules can be used in a single application. For example, Spring Data - JPA in a Spring Boot application. I've got apps that have used 4 or 5 different modules in one app, in fact.

    For a good introduction to Spring, Manning Publications "Spring In Action" book is recommended.
     
    Nathan Milota
    Ranch Hand
    Posts: 351
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have the book "Spring In Action".  I'll have to start reading it again, it seems.  

    What I was mainly getting at is, Spring Boot seems to eliminate the need to use anything such as a xml file, bean factory, component scan, etc... So, is there still a point to learning it, and why?

    We don't learn frameworks in college or even touch the surface of them.  The local courses that have labs where you learn Spring and Spring Boot in person all cost thousands of dollars that I don't have, but what I would really need is something like in person, hands on training with it to understand it.  I'm hoping whatever company I am able to join eventually has some kind of training that clears up some of the gray areas.  
     
    Tim Holloway
    Bartender
    Posts: 21731
    148
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't count myself as an expert in Spring Boot, but I doubt very seriously that that it eliminates the BeanFactory. In fact, odds are that one of the first things that the Spring Boot Bootstrap class does is obtain a Tomcat server bean from the BeanFactory and initialize it, deploy the webapp into it, and launch Tomcat.

    A lot of the processes and configurations outlined in Spring In Action may have been assisted by code that's a standard part of Spring Boot, but I think that if you dig, you'll find that they are still there. And you'll be better off knowing that they're there when it comes time to do things that the standard Spring Boot setup cannot handle.
     
    Nathan Milota
    Ranch Hand
    Posts: 351
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Say in my example, I have a Student class, and when a Student is given a Ticket, they have an object of the Ticket class.   Those are tightly coupled.  How do you use dependency injection there?
     
    Tim Holloway
    Bartender
    Posts: 21731
    148
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Nathan Milota wrote:Say in my example, I have a Student class, and when a Student is given a Ticket, they have an object of the Ticket class.   Those are tightly coupled.  How do you use dependency injection there?



    You don't. Dependency injection is part of the construction process. That "is given" is an indication of a post-construction action. Students and Tickets would, presumably, be JPA Entity Objects, which are constructed via the "new" operator, not obtained from a BeanFactory. BeanFactory mostly returns singleton objects, and Entities are rarely singletons.

    In your case, you might have a separate Ticket factory that constructs a Ticket for a particular school. Such a factory would initialize the Ticket's ID components, giving a unique ticket ID from a sequence constructor for each school ID passed to it. Although there are other ways to do that, including some rather gnarly techniques within JPA itself. You probably wouldn't be using the Spring BeanFactory, though. It's more geared towards producing stuff from fixed specifications. And, as I said, generally Singleton instances, not multiple parameterized Entity Instances.

    Note: "Dependency Injection" is not the same thing as "Injection". A Dependency Injection is an Injection done because Spring has been told that the object being requested references other object(s), which then have to be located/created and Injected into the object being created. Dependency Injection is only done at construction time. General Injections can occur at any time (allowing for possible multi-threading issues).
     
    Nathan Milota
    Ranch Hand
    Posts: 351
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is there an online web course or anything other than just someone's video channel on YouTube for learning Spring and Spring boot you'd recommend?  I personally just don't like "Spring in Action".   It's entire book is about building a taco application, and I think many of its tools might be out of date as the code as written in the book simply does not work and I don't find it to be a very good book for a beginner as it's very confusing.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11472
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you tried Spring's own user manual, and their quickstart and tutorials?
     
    I love a good mentalist. And so does this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!