I am working on an interface for making CRUD operations using JPA, and the following question formed in my head.
If I use persist for my create method, then I can catch an EntityExistsException, if the method is called with an object that has an ID already in the database.
In the update method I will then use merge for saving changes. In order not to create something that does not exists I was thinking about looking it up first, and throwing an exception if it is not found in the database.
Now I am just thinking that this might be overkill, and why not just let merge create it if does not exists and update it if it does? But then what do I need the create method for then?
So what do you think? Is it best practice to have a create method that only creates and throws an exception when trying to create something already in the database, and have an update method that only lets you update something that already exists and therefore never creates?
I'd use just merge. If you understand (and document in your code) what it is doing, it is a good option. I've used it on several projects without any problem.
Related
Background: I have few models which are materialized as 'Table'. This tables are populated with wipe(Truncate) and Load. Now I want to protect my existing data in the Table if the query used to populate data is returning empty result set. How can I make sure an empty result set is not replacing my existing data in table.
My table lies in Snowflake and using dbt to model the output table.
Nutshell: Commit the transaction only when SQL statement used is returning Not empty result set.
Have you tried using dbt ref() function, which allows us to reference one model within another?
https://docs.getdbt.com/reference/dbt-jinja-functions/ref
If you are loading data in a way that is not controlled via dbt and then you are using this table - this is called a source. You can read more about this in here.
dbt does not control what you load into a source, everything else that is the T in the ELT is controlled where you reference a model via ref() function. A great example if you have a source that changes and you load it into a table and make sure that incoming data does not "drop" already recorded data is "incremental" materialization. I suggest you read more in here.
Thinking incremental takes time and practise, also it is recommended every now and then to do a --full-refresh.
You can have pre-hooks and post-hooks that can check your sources with clever macros and add dbt tests. We would really need a little bit more context of what you have and what you wish to achieve to suggest a real response.
I am having a problem with django subqueries. When I fetch the original QuerySet , I specify the database that I need to use. My hunch is that the later subquery ends up using the 'default' database instead of what the parent query used.
My models approximately look like so (I have several):-
class Author(models.Model):
author_name=models.CharField(max_length=255)
author_address=models.CharField(max_length=255)
class Book(models.Model):
book_name=models.CharField(max_length=255)
author=models.ForeignKey(Author, null = True)
Now I fetch a QuerySet representing all books that are called Mark like so:-
b_det = Book.objects.using('some_db').filter(book_name = 'Mark')
Then later somewhere in the code I trigger a subquery by doing something like:-
if b_det:
auth_address = b_det[0].author.author_address
My problem is that arbitrarily in some cases , on my live server, the subquery fails even though there is valid data for that author's id. My suspicion is that the subquery is not making use of the same database 'some_db'. Is this possible? Is it so that the database that needs to be used is not sticky in subqueries? It is just a hunch that this might be a problem, it is happening in the context of a celery worker, is it possible that the combination of celery with django ORM has some bug?
I have solved this each time this occurred by doing a full fetch by invoking select_related like so.
b_det = Book.objects.using('some_db').select_related('author').filter(book_name = 'Mark')
So right now, the only way for me to solve the problems is determine beforehand all the data that I will need, and make sure that the top level fetch has all those inner model references using select_related. Any ideas why something like this would fail?
I am unable to recreate this locally else I would have debugged it. Like I said, it is pretty random.
Ok, I have an handle on this now. My assumption that the subqueries would remain sticky to the original database is wrong. What django does is that first it hits the database router that is configured. If that does not return anything only in that case it makes use of the original database.
So, if the configured database router returns some database to be used then that gets used. In my opinion this is wrong and we need to use the original database first and then check the database router.
So Im new to databases and Im trying to learn the ropes. I have a DB2 database that Im getting familiar with. I was assigned a task where I need to write a method that does a search on the database. The search will take in two parameters, a username and a user id number. If the user and the user id number does not match or if one or the other turns out null then It needs to throw a error. If its valid then it will continue with spitting out information about the user.
I was told to use the findall() function or something similar to it. I was looking online and what I have found is examples that deal with like or ilike and im not sure how something like that will work in my situation. What would be a decent example of how I would start to go about this?
any help is appreciated. Ill post back if I make any progress.
note: Im using groovy/grails. Domain,Controller,View setup.
Is this some homework assignment from school?
findall() is usually a method in regular expressions which I don't think is relevant in here. If you have a SQL database, that means you have a RDBMS which uses SQL as query language. You need to learn about the SELECT command which can look daunting when you look the first the time to the manual but it's actually simple for your case. You need something like:
SELECT userfield1, userfield2,..
FROM myusertable
WHERE myusertable.username = 'uname' AND myusertable.userid = userid
uname and userid are your search parameters. Please note that this SQL query should be done with a PREPARED statement for security reasons.
When you run this query using your database library you get back an array of results which you have to analyze. If it is empty, no user found.
Edit: updated to take into account the use of hibernate
Hibernate uses HQL which is like SQL and has indeed a findAll method. See http://grails.org/doc/latest/ref/Domain%20Classes/findAll.html
In python::Pylons i'm able to issue a setup-app command and it will look at my Models and issue the appropriate CREATE TABLE or CREATE INDEX ddl for my particular database.
it seems like this would be a feature in CakePHP, but i'm having trouble finding it.
in fact i see this in the manual:
"You can create your database tables as you normally would. When you create your Model classes, they'll automatically map to the tables that you've created."
which leads me to believe it doesn't exist?
No, it's other way around - you can create models, controllers and views by having DB schema. It's more logical to have a DB design schema first.
Check this out
Some of the comments in the accepted answer above lead me to creating this answer. You can technically create new tables on the fly using the YourModel->query() function. I am currently using this in a Behavior I am writing. This works in CakePHP 2.x, pretty sure it works in 1.3 as well.
In the setup function for the Behavior I am checking to see if the table already exists. If it doesn't I create it.
$dataSource = ConnectionManager::getDataSource('your DB');
if(!in_array($tableName, $dataSource->listSources()){
$this->createYourTableFunction();
}
In the createYourTableFunction you create a temporary model to run the YourModel->query() against. And just provide it your SQL instructions. When creating your temporary model just set the table parameter to false so you don't get a missing table error.
$YourModel = new Model(array('table' => false, 'name' => 'YourModel', 'ds' => 'Your DB'));
$YourModel->query('SQL instruction string');
Does any one know how to create a view from hibernate with the results of a criteria query?
We've got some legacy parts of our application that use views generated by the app for data retrieval and I like to tie the new NHibernate stuff into those for minimal friction.
I'd turn it into an extension method so I could eventually do stuff like this:
session.CreateCriteria<Thing>().CreateReportView().List();
Any ideas?
The existing process is like this:
SQLString = _bstr_t("SELECT name FROM User WHERE Retired = false");
...run the query process the results, then...
SQLStringView = _bstr_t(" \
BEGIN EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW ") + ViewName + _bstr_t(" AS ") + SQLString;
So whenever we run this query we get a view that has the same data in it. I can't work out how to replicate this is hibernate though.
To create a view using NHibernate directly, take a look at the 'database-object' mapping element.
Ayende has a good example here.
Check out this article for an explanation of mapping an entity class to a view and a table. I'm not certain that you'll be able to dynamically create your views at runtime as you specified; but perhaps this can be done as part of the schema generation process using the database-object mapping?
If you're only interested in filtering the data being returned, you may want to have a look at Nhibernate's filtering mechanisms; here is a good article outlining their usage.