I am to the topic of Hibernate. I am working no on validation system now. I would like to search the database, whether data entered by the user already exists in it, such as login. How to do it using hibernate?
Please check following link: http://gwt-vl.sourceforge.net/?to=serverDoc
Esspecially the section: Server side to client side validation This may help you to validate your form on the server side.
If you need to know about communication with backend I would recommend (GWT_PLATFORM)[http://code.google.com/p/gwt-platform/] it's a great framework by Philip Beaudoin and also some highly respected by GWT team and was infect release before GWT MVP . Okay you already have that sorted out then what you will need to work out what combination of UI fields is a unique database entity for example Name and Date of Birth something along these lines. Then before you persist this data you will using your DAO (I am assuming you have a DAO layer) search for that object (you can use JPA QL even with hibernate or Hibernate Criteria) if your search (and user doesnot know about it just a back end process) does not return a result then you can just persist then new information but if it does then it will be upto the business rule weather you show user and error or update the information.
Related
I've started exploring axon framework.
I'm following this tutorial
It is basically creating an account, retrieving information about the account and few other activities.
Whenever an event takes place, an entry is stored in Domain Event Entry Table. The payload is also present in hashed form . My questions are :
Is it possible to access the entries present in Domain Event Entry table ?If yes , then how?
Also, If I want to add the query and do log it in the console, does the payload appear in hashed form or in the original format.
Any help will be appreciated.
Checking the project you shared, I would start by upgrading the version you are using. I can see it's using 4.4.3 while latest is 4.5.3 and it brings lots of improvements and new features.
Before going to your questions, I believe it is important to note some things here.
This project is NOT using Axon Server, so your Events are stored on a Database of your choice. In this case, since you have JPA on your classpath, Axon Framework will automatically configure a JpaEventStorageEngine for you. Similar to the TokenStore implementation.
It is important to know about the Serializers as well, and for that I will refer to the official docs. Configuring them, you can have your Events stored as XML or JSON.
So, to your questions now:
Yes, you can access them as you would do in a normal database. Since I can see on the project it has /h2-console enabled, going there would be my first choice. Alternatively, you can configure any tool/app capable of checking the contents of your database.
For logging the query, you have to configure Hibernate for example for logging that. In any case, it will log it as a deserialized object and not as a 'payload' form. Of course, you can write some queries for the DomainEventEntry table and log the payload but that's not how you are supposed to use that table.
For more info, I would recommend going to the official docs or to our Axon Academy!
Edit 1: adding info about QueryHandler
Axon Framework offers, as you noted, #QueryHandler annotation which should be part of your Query side (appart from the Command side, derived from CQRS concept). That is the side responsible for providing information based on Events (from your Event Store a.k.a. DomainEventEntry in your case). Basically, Events will be propagated to the Query side where you will have one or more EventHandlers components/methods responsible to handle the Event and write the derived result to a form of storage (usually a relational database). You also have one or more QueryHandlers components/methods that will get Query messages, perform the query it (using a Repository for example) and return the response to the caller. In that sense, you can really tailor your query side the way you want! The ref guide has some info about Query Handlers that I recommend!
You can access the events in the domain events table. With H2 you can log in to the H2 console and find it there.
If you use a query the payload will be deserialized (no XML or JSON) and can be used to log information or whatever you need to do.
I have a Java XPages application with a REST service that functions as an API for rooms & resources database (getting appointments for specific room, creating etc).
The basic workflow is that an HTTP request is being made to a specific REST action, having the room's mail address in the search query. Then in the java code I'm iterating over all documents from the rooms & resources database, until I find a document with the InternetAddress field with the searched mail address.
This isn't as fast as I would like it to be, and there are multiple queries like this being made all the time.
I'd like to do some sort of caching in my application, that when one room is found once, it's document UID is being stored in a server-wide cache so next time a request is made for this mail address, I can directly go to the document using getDocumentByUNID(), which I think should be way faster than searching over the entire database.
Is it possible to have such persistent lookup table in Java XPages without having any additional applications, while keeping it as fast as possible? A hash table would be perfect for this.
To clarify: I don't want caching in a single request, because I'm not doing more than one database lookups in a single query, I'd want to keep the caching server-wide, so it would be kept between multiple requests.
Yes, it is possible to store persistent data. What you are looking for is called an application scoped managed bean.
I would like to save in my database information about history, for example user "dog" edited field "grass" in table "garden".
I have trigger which saves everything correctly but I have problem with username "dog". Username is logged user's name and I don't now how to "catch" it, because I don't know how to tell my database (PostgreSQL) that this specific user did that.
How can I tell my trigger that it should use value "dog"?
I would like to write an application in Java using Spring Framework and Hibernate Framework. I haven't any app code, because now I'm creating database and thinking about my future application.
Any ideas?
For certain database platforms, they offer context parameters. To use these, you would:
Set the database context parameters.
You can simply use the native SQL interface exposed by Session or EntityManager to accomplish this step.
Register an AfterTransactionCompletionProcess with the Session.
This should basically use the provided Session and clear the database context parameters which you set as part of (1). You would want to do this regardless of whether the transaction was successful or not. This step makes sure those context parameters are cleared prior to giving the JDBC connection back to your connection pool.
Execute your normal ORM changes.
But there is probably a much simplier approach all together, called Hibernate Envers.
Hibernate Envers is designed to mirror your mapped #Entity classes and keep a running history of changes made to your entities. You can easily configure the fields you'd like audited should there only be a subset of fields you're interested in the history on. Additionally, the Envers API exposes an easy way for you to query the history tables and get historical snapshots.
In order to store your username "dog" with Hibernate Envers, you would need to merely implement a custom RevisionEntity that contains your userName field and set it. You can find more information on how to configure the necessary components for this here.
We are developing a new web application. one of the most basic requirement is to audit all entities changes into a separate table.
We would like to use DB triggers for that purpose.
We use MySQL as our RDMBS.
The problem we now foresee is that whenever a trigger is pulled, and insert a new entry for the DB, it cant possibly know the (applicative) user that made the change. (all users have different ids, but spring uses a single user account for the db manipulations.)
Any ideas how to resolve this issue?
We resolved the issue by adding a field to all tables that are being audited of the userId, and on each CRUD operation we made it mandatory to provide it. (for system business logic we use id=0). this way our audit table are being populated with the id itself to be monitored.
I'm multing a multi-tenant SaaS web-application in Java, Spring, Struts2 and Hibernate. After a bit of research, i choose to implement multi-tenancy in a shared db, shared schema, shared table approach. And tagging each db-line with a tenantId.
I have rewritting my application, so Managers and Dao's will take the tenantId as a parameter to only serve the correct db-resources.
This works perfect for all view's when getting information. And also for creating new stuff (using the logged in users tenantId to store the info).
However, for updating and deleting stuff I am not sure how to secure my application.
For example: When a user want to edit an object, the url will be: /edit?objectId=x
And this is mapped to an action that will retrieve this object by Id. Meaning any logged in user can by url-modification view any object.
This i can solve by adding the tenantId to the Dao so if the User tries to view an object outside his tenancy he will get nothing.
Ok thats ok then, but about when sending in the edit-form?
What if the user modifies the request, messing with the hidden field objectId so the action will receive a request to alter an object not belonging to the users tenancy.
Or if the users url-modifies a delete action /delete?objectId=x
Basicly I need some way of assure that the logged in user has access to whatever he is trying to do. For all get's its easy. Just putting the tenantId in the where clause.
But for updates and deletes i'm not sure what direction to go.
I could query the db for every update and delete to see if the users has access to the object, but i'm trying to keep db-interaction to the minimum. So i find it impractical to make an extra db-call for every such action.
Does anyone have any hints or tips to my issues?
The same for reading applies to writing/updating: user can only see/access/change what they own. Your question is more about database that about anything else. The same constraints you apply to viewing data must also apply to writing data.
In this case, you don't want to wear the performance of a query first then an update. That's fine, since you can update the database with conditions. Since this seems likely to be database-level in your case you need to know what your database is capable of (to do it in one go). For example, oracle has the merge statement.
I am quite late to this thread and maybe you have already built the solution you were asking here about. Anyway, I have implemented a database-per-tenant multitenant web application using Spring Boot 2 and secured the web access using Spring Security 5. The data access is via Spring JPA (with Hibernate 5 as the JPA provider). Do take a look here.