[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/database/

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 1

File: 48948948.png (45KB, 1280x662px) Image search: [Google]
48948948.png
45KB, 1280x662px
I think I'm doing databases wrong.

I have a pretty big Java project I'm working on and now that I'm at the part where I have to start storing shit in a sql database, my whole workflow got fucked up.

Do I actually have to create a fucking connection to the database, create a prepared statement, interpret the resultset, close the resultset, close the prepared statement, close the connection every time want to do anything with the database?

I want to keep my Model stuff separated from my Database stuff so do I really have to make a separate database class for every Model class, painstakingly explaining how to convert this Java object into it's relational parts in the database and the other way around?

Not even to start about all the stuff I have to think about when I want to make a small change to some object and have to change a gazillion things in relationship tables.


How do people do this shit? Am I missing something? I'm lost.
>>
>>57012010
do you go to ship?
>>
>>57012175
Come again?
>>
>>57012213
nevermind. I'm a student at ship, and we are doing pretty much the same thing in my architectures class.
>>
ORM ist you friend
>>
>>57012225
How do you solve this shit?

>>57012244
I'm afraid to use it because I keep reading stuff like this:
http://stackoverflow.com/questions/452385/what-java-orm-do-you-prefer-and-why
>>
Create a static DatabaseHandler class and a DatabaseHandlerFactory to get references to it :)
You chose to be ENTERPRISING anon
>>
>>57012261

well first off, you should have a gateway class, that acts as a well, gateway, to the database. It handles connecting, closing, rolling back transactions, starting transactions, etc... then all connections and such will happen through the gateway.

pretty much you have a domain layer, then a mapping layer (takes care of mapping the domain to the data source and vice versa), and that's it. there are several ways in which you could implement the mapping. if you really want to make things super clean, then the domain should have zero knowledge of the data source.

domain <- mapping / persistence -> data source

also, you could factor out a lot of duplication in your mapping / persistence layer.

it can get pretty complicated. here's a good paper -

http://www.agiledata.org/essays/mappingObjects.html
>>
>>57012244
ORM is a meme. Tables of a properly normalized database almost never map to domain objects 1:1.

Yes, writing data mappers is a pain in the ass, but if you don't want to end up with terribly designed DB and/or business logic, as well as inefficient as fuck queries, it's the only way to go.
>>
>>57012325
Thanks, I will definitely read that paper.

At the moment I was thinking about doing this:

Give every Class a public static Orm object. Obj is an interface that has stuff like "add to database" and "update" and shit

So something like Apple has a public static AppleOrm object that implements the Orm interface and also has extra ORM functionality specific to apples.

Sounds alright to you?

( I know it's not 100% clean because there's a tiny bit of Model/Mapping overlay, but I don't mind making that little sacrifice if it will speed up my workflow)
>>
>>57012465
open closed principle nigga

Just write the class to handle ALL things SQL first and see if you can work with it. If not, keep improving it until you can. THEN you can integrate it into your project.
>>
>>57012281
This is what I tried at first, but I found out day 1 that DatabaseHandler would be 5 million lines long. So now I'm thinking about doing >>57012465
>>
>>57012485
>Just write the class to handle ALL things SQL first and see if you can work with it.

Already did this. The answer is "no".
>>
>>57012508
Then you can't SQL and have some learning to do. You came here for advice, you got it.
>>
>>57012010
>connection to the database, create a prepared statement, interpret the resultset, close the resultset, close the prepared statement, close the connection
Pretty much, yes. The close statements will cascade though, closing a statement will close any resultsets it created etc. Read the documentation for more information.

Also you don't want to create a new connection for every query, the SQL server will crap on you very quickly if you do that with any decent userbase. What you should have is a ConnectionManager class that holds a connection pool and hands connections one by one. In small projects you can just have one actual connection that stays open untill you close the program.
>>
>>57012530
No, I mean I can do it, but it's extremely inefficient so I started looking for another solution pretty soon.
>>
Back when I did manual database stuff with Java it looked something like this:
public List<User> getUsers() throws CustomAbstractionException {
try (PreparedStatement stm = conManager.getConnection()
.prepareStatement(statement_as_static_final_variable)) {
stm.setString(1, "whatever");
ResultSet res = stm.executeQuery();
List<User> list = new ArrayList<>();
while (res.next()) {
list.add(new User(
res.getString("name");
res.getString("foo");
));
}
return list;
}
catch (SQLException sqle) {
throw new CustomAbstractionException(sqle, "some sort of default description");
}
}


So basically use holder objects to transfer data back and forth from application layer to database layer. And abstract everything, do the whole thing as a class that implements your own database interface and keep the SQL exceptions away.

I havent touched actual SQL code in ages though, Spring JPA is good enough for me. Might be that you're supposed to use it differently.
Thread posts: 17
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.