This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Can we have validations like this if we are not using JPA ?  RSS feed

 
Ranch Hand
Posts: 468
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this validation method in the following link and I thought it is really cool.

https://stackoverflow.com/questions/27867713/patterns-for-data-validation-when-using-a-dao

My problem is that I am not using JPA, just pure Java EE.

Can I use that method ?
 
Master Rancher
Posts: 881
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To quote most of the mods here:

Don't post just a link, a lot of the users are hesitant to click it.


You could have as easily posted a snippet from that link to show what you mean...
 
Daniel Demesmaecker
Master Rancher
Posts: 881
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The snippet:

The short answer is yes, but...
The slightly more extended version is, when using jdbc you have to write your queries yourself, so you're also responsible for the validation of the date.
The validation would happen before you use it in a querry, I don't know how you get your data, but if you're using mvc, that would be in your controller.
@id, @GeneratedValue and @NotNull, you don't need if you're db is set up correctly. id is the primaryKey, GeneratedValue is autoIncrement and NotNull is NOT NULL, those you would set up in your table on creation.
Checking if the value matches a certain pattern, you can do in you controller: @Temporal and @Past, I would never use, it simply means that the date is undefined and should be in the past.
You can simply make your column of type date and to check if it's in the past you can use:
which return a boolean
 
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:My problem is that I am not using JPA, just pure Java EE.

Can I use that method ?


JPA is part of JEE, so if you're using JEE you can also automatically use JPA.
 
tangara goh
Ranch Hand
Posts: 468
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:The snippet:

The short answer is yes, but...
The slightly more extended version is, when using jdbc you have to write your queries yourself, so you're also responsible for the validation of the date.
The validation would happen before you use it in a querry, I don't know how you get your data, but if you're using mvc, that would be in your controller.
@id, @GeneratedValue and @NotNull, you don't need if you're db is set up correctly. id is the primaryKey, GeneratedValue is autoIncrement and NotNull is NOT NULL, those you would set up in your table on creation.
Checking if the value matches a certain pattern, you can do in you controller: @Temporal and @Past, I would never use, it simply means that the date is undefined and should be in the past.
You can simply make your column of type date and to check if it's in the past you can use:
which return a boolean



Hi Daniel,

I would like to clarify with you this line which you have written :


Checking if the value matches a certain pattern, you can do in you controller:

1

value.matches(pattern);



Are you saying that we can check the pattern of regex of that string, after we get the particular value ?

Do we just put the regex at the Model class will do ?

How will the Controller be able to tell which pattern belongs to which attributes ?

Or we have to write a helper class for that ?
 
tangara goh
Ranch Hand
Posts: 468
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

tangara goh wrote:My problem is that I am not using JPA, just pure Java EE.

Can I use that method ?


JPA is part of JEE, so if you're using JEE you can also automatically use JPA.



Hi Rob,

I hope I understand you correctly.


So, what you are saying is that I can add JPA on top of the JDBC with all the queries, is that right ?

Is this what people do in the industry ?

Because from what I know to use JPA, you also need to add annotations such as one to many and many to one, on the attributes concerned etc.
 
Daniel Demesmaecker
Master Rancher
Posts: 881
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

name="email"  id="email" placeholder="Enter your email" class="form-control" required="required"/>


It depends on what you're trying to validate, if it's an email, you can do that directly in you form:

And in the code snippet he shouldn't even have used a pattern, he could have used @Email
for example a telefoonnumber you would put the pattern in your controller:

So, what you are saying is that I can add JPA on top of the JDBC with all the queries, is that right ?  


JPA is an interface, so you could, but you still need an implementation of JPA, like hibernate for example
 
Rob Spoor
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each JEE container like JBoss, GlassFish or WebLogic needs to come with one though. You would only need to find out which one so you can put the correct provider in your persistence.xml file. (JBoss comes with Hibernate, WebLogic with TopLink which is based on EclipseLink, no idea about others.)
 
tangara goh
Ranch Hand
Posts: 468
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Each JEE container like JBoss, GlassFish or WebLogic needs to come with one though. You would only need to find out which one so you can put the correct provider in your persistence.xml file. (JBoss comes with Hibernate, WebLogic with TopLink which is based on EclipseLink, no idea about others.)



I am using Tomcat, which I believe is the cheapest solution when comes to hosting.

Alright, I will research how it can be done.

So, just to confirm, that it is normal to add JPA on top of the normal jdbc ?

Cos I know that JPA handle ORM so I always thought it is choosing either the jdbc way or the JPA way or the Spring Data way or the hibernate cum Spring way.

 
Rob Spoor
Sheriff
Posts: 21550
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's advised to use a higher level mapping like JPA or Spring's mappings (Spring JPA, Spring Data, ...) instead of JDBC. It's still possible to use JDBC though. You just don't get any of the advantages of JPA. And JPA also allows native statements to be executed; if you want their result sets mapped you just need to add a little more code.
 
Bartender
Posts: 20098
101
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:
Because from what I know to use JPA, you also need to add annotations such as one to many and many to one, on the attributes concerned etc.



No. You don't. But you will have a lot less fun if you don't.

JPA is, in large part, a way of making it possible for people who know Java but don't know SQL very well to do complex database operations. First and foremost of those operations is the insidious JOIN, which can be a major source of frustration even for those of us who have been doing it for many years. JPA replaces the SQL join with the @OneToMany, @ManyToOne, etc. annotations which require no actual Java code to not only get your join but to link the joined records in a way that makes it easier to write the application.

It's not really recommended to mix raw JDBC with JPA. JPA generates JDBC from the SQL-like JPA Query Language (JPQL), and because it has done all the work itself, it knows how to optimize the overall database workload for that application. Direct JDBC won't get counted into those calculations. Also, if you're not careful, the caching and delayed operations in JPA may end up undoing JDBC commands you have done.

Also, not all JEE servers have built-in JPA. The Tomcat and jetty webapp servers don't. It's easy to work with JPA in Tomcat, but you have to supply your own copies of the JPA libraries when you build the WAR.

JPA is very powerful, but it's also very complex. It makes simple things simple and complex things possible. But because it's a larger investment in time and effort, it works best for heavy-duty applications. Then again, the same thing can be said for Java itself. For simple stuff, it's much quicker to use something like PHP, python or nodejs.
 
straws are for suckers. tiny ads are for attractive people.
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!