I cannot find the difference between to two in my search on SO. Can someone explain the difference to me? I feel pretty confident on my understanding of an ORM. But I do not know what SQL Mapping is, and therefore how it is different from an ORM.
I am using Java if it matters.
I control all of the database.
Is there an example of a small understandable SQL Mapper I could look at?
EDIT: I really do not want to embed SQL in my code. That's the devil.
Related
I have been learning Entity Framework Core for about two weeks. I kind of understand how to use it. But when it comes to querying. The query that'is generated from LINQ looks so crazy. It has so many unnecessary duplicated left joins. I cant post my query here and have people help me to optimise it. But I want to know are there a documentation or some article that I can read to understand how queries are generated from LINQ. And all the ways to optimize one query at LINQ level not talking about indexing and stuff related to database.
I would like to create my own query language for a web api I wrote. But I have no idea where to start for it.
The language should be like SQL. For that I looked up the NHibernate code, cause I know, that they have the HQL (Hibernate Query Language) but it didn't help.
Are there any instructions or sth.? If this question is wrong here please move and/or tell me where I should ask else.
The first step is a lot of design work, starting be answering the following question:
Is this new Query Language going to be converted to SQL which will be
executed by a standard database engine, or are you going to write your
own database server as well?
If it's going to be converted to SQL (just like HQL) then map out the translations from your language to SQL on paper (and make sure you know the possible SQL constructs you may have to use). Once you've got that, you can start implementing it. Yes, this sounds like BDUF, but the language should be defined in one pass, I think, as it will be more consistent and easier to use if you do it that way. You can always implement it in a more Agile way once you've got that.
If you're going to write own database server, you're on your own....
I have no experience in SQl databases. I just started using ActiveRecord and I find myself compy pasting the ActiveRecord query statements beucase I don't know difference between when to use .find() .where(), joins etc. There's so many ways to do one query and I am ovewhemled. I am just wondering if it's a good idea to take a step back and learn SQL first or jus try to follow along? Also if what's the best way to learn SQL?
Thanks
Yes! You should definitely know SQL to use ActiveRecord effectively.
AR improves productivity by abstracting you away from the tedium of writing raw SQL, but an understanding of what it's doing will improve your usage of it, as well as your ability to write effective code.
You could start by watching your logs with tail -f log/development.log while you work, which will show you the statements generated by AR. This will help you gain a frame of reference to help you see what AR is doing for a given call.
AR doesn't replace SQL - it augments it by letting you use it at a higher level. Understanding what's going on under the hood will only help you.
I would definitely recommend learning SQL first, it will help you out ALOT in the long run. Active Record really is just an Object-Relational Mapping of the database that helps to make writing the SQL cleaner, clearer, and easier. That being said I do not think it is a replacement to know SQL, not to mention it is pretty easy to learn the basics. I would suggest looking at both documentation on SQL as well as active record.
SQL: http://www.w3schools.com/sql/default.asp
Active Record: http://guides.rubyonrails.org/active_record_querying.html And http://api.rubyonrails.org/classes/ActiveRecord/Base.html
If you know SQL basics then it's easy to use ActiveRecord.
To learn SQL basics you can use this link.
To learn ActiveRecord use this link.
So I'm having a head against the wall moment and hoping somebody can come help either remove the wall or stop my head from moving!!
Over the last 3/4 weeks I've been investigating ORM's in readyness for a new project. The ORM must map to an existing, large and ageing SQL database.
So I tried Subsonic. I really liked v2 and v3 after modding to work nicely with VB and named schemas in SQL was running OK. However, its lack of flexibility of having separate entity properties names vs column names had me pulling my hair out (sorry Rob).
I tried Entity Framework but I found like others it lacking in certain areas.
So I bit the bullet and tried nHibernate but after a week or so getting it working how I liked (with help from Codesmith to generate classes/hbms for me) I'm frustrated with the time it takes to startup (build a config object), despite trying a number of tricks to reduce this time.
I'm essentially after building a DAL class that I can share between apps and websites. Am I barking up the wrong tree? For a legacy project with 100s of tables should I go back to ado.net and use DTOs? Aarrgh!
Sorry for the ranty style of question. I don't have much hair left and I'd like to keep what I have!!
Thanks in advance, Ed
PS. I should add that I know SQL very well and not scared of getting my hands dirty to write fast queries. If anything I don't need to be hid from SQL
ORM let's you:
To map table rows to objects, that are the the workable pieces of object oriented programming.
To automatically navigate through object relationships
To easily add, edit and remove table rows
To query the database in a more intuitive way as you don't have to think of joins (this one will depend on the ORM and the query method)
To transparently handle L1 and L2 cache.
All of the above would have to be handled by hand if you werent using ORM.
PS: I agree to Dmitry as to the startup time of NHibernate (see question comments). Besides, did you try Fluent NHibernate? Fluent NHibernate is impressively easy. I couldn't believe my eyes when I first mapped a database. It's even easier than proprietary ORMs like DevExpress XPO.
The biggest benefit of an ORM tool is that it will help you layer your application correctly. Most project nowadays use a Data Layer to connect to the database. You start from the ORM tool to produce classes that correspond to your database objects. Then you define an interface using these methods. All persistence code uses the methods of this interface. This way the business logic layer is only coupled to this higher-layer interface and needs to know nothing about the database. In fact there should be no dependency on ADO.NET or even NHibernate.
Another advantage of ORM tools is that you de-couple your application from the database server. You could change the db engine and still use the same code. Also there isn't only the complexity of the SQL that the ORM hides from you. It can also help you with transactions logic and connection pooling.
I'd say that for new projects an ORM tool is a necessity. For legacy projects it isn't so much beneficial, unless of course you have the time/money to start from scratch.
In my experience, most ORMs end up being way more complex than SQL. Which defeats the entire purpose of using them.
One solution I'm enthusiastic about is LINQ2SQL. It excels as a thin layer about stored procedures or views. It's really easy to use and doesn't try to hide SQL.
There are basically two questions here:
What's great about ORMs? There are similar questions on Stackoverflow. See:
What are the advantages of using an ORM?
Is everyone here jumping on the ORM band wagon?
How can I improve NHibernate startup time? See:
http://ayende.com/Blog/archive/2007/10/26/Real-World-NHibernate-Reducing-startup-times-for-large-amount-of.aspx
http://nhforge.org/blogs/nhibernate/archive/2009/03/13/an-improvement-on-sessionfactory-initialization.aspx
I know that in most cases it`s more useful to learn more complicated technology/language and then easy one than vice versa.
But, actually, time to do university tasks is limited. If I first learn LINQ, then go for SQL, would it be hard for me to use and learn sql?
EDIT
The task I need to do is to work with database and get some data from it, so the question is almost about LINQ to SQL.
It is a bad idea.
And if today's universities teach you LINQ instead of giving you foundation to build your knowledge upon, I can only feel sorry and pity for their students.
Time is always limited. Don't waste it on things that are subject to constant change.
SQL will be there tomorrow, LINQ.... well who knows.
SQL is applicable anywhere, LINQ only in .NET world.
Either LINQ or something else, it will be easy to "learn" it afterwards. When you have the knowledge of SQL, it will just me a matter of hours/days/weeks, hardly longer.
Well, the 2 things are very different. LINQ (in the pure sense) isn't actually related to databases at all - it can be used quite happily just with in memory objects, or against web services, etc.
If you are mainly interested in writing better .NET code, then learn LINQ - but learn it properly - perhaps pick up C# in Depth, for example - which covers it very well in the last few chapters.
If you want to know about databases, then sure: learn SQL (such as TSQL) - but understand the differences. Databases are good if you need to write enterprise software, but not necessarily if you just want to do simple tasks.
edit re edit to the question
If you are just getting simple data in and out of the database, then you probably don't need to know much about SQL. Just use LINQ-to-SQL (or whatever tool), and let the ORM tooling worry about it.
Learn SQL first, then LINQ.
That way you'll understand how LINQ-to-SQL is working behind the scenes, but you'll also know enough to be able to cope when LINQ can't do what you need.
SQL is a standard, learn the standard.
More precisely :
learn database theory
learn CODD algebra
then pick up a "common database", do some tutorials on it, ...
I personnaly really like PostgreSQL tutorial
I submit that you cannot effectively use LINQ unless you have SQL knowledge. If you don't understand, at a minimum, the following, you cannot effectively query a database in any fashion:
select
insert
delete
update
joins
group by
boolean algebra
relational theory
set theory
Learning SQL will give you the concepts you need even if you use LINQ later.
Sql. However you could play with linq pad for a while - it is a freeware and realize that LINQ is a nice hybrid between SQL and C#