JPA newbie can't interact with database - java

I started ussing JPA not long ago and I think it is great because of its annotations that make programming so simple. But I still have a lot few doubts that slow me down a bit (most of the times regarding to configuration issues):
I would like to understand the difference between the derby DB that comes in the OEPE(Oracle Enterprise Pack for Eclipse), I was using this for my learning, and the popular MySQL DB(www.mysql.com). What confuses me is what is the difference with an embedded DB an a non embedded. What is better, what do you recommend?
The second doubt is regarding to some configuration issue I can't fix with my derby DB(not embedded mode) I already made a question, but I could not fix it. The problem is that when I try to access some of the tables of my DB(The tables exist in the database, they were created by the JPA feature generate Tables from entities), I get SQLSyntaxErrorException saying that the tables don't exist. I know that is impossible because that same query manually inserted in eclipses scrapbook works correctly. I know this have something to do some configuration issue, but I don't know what is it.
I'll be really happy if someone with a bit more experience with JPA could give me a hand.
The tools I use for my programming are eclipse + OEPE, Java EE 6 and GlassfishV3.0
This is the link to my other question where I explain the issue in more detail:
SQLSyntaxErrorException: Table/View 'BUYER' does not exist. What is missing?
Thanks in advance.

Regarding question 1:
Derby/JavaDB has two operation modes (quoted form JavaDB documentation):
Embedded
Refers to Derby being started by a
simple single-user Java application.
With this option Derby runs in the
same Java virtual machine (JVM) as the
application. Derby can be almost
invisible to the end user because it
is started and stopped by the
application and often requires no
administration. The Derby
documentation often refers to this as
the embedded configuration or embedded
mode.
Server (or Server-based)
Refers to Derby being started by an
application that provides multi-user
connectivity to Derby databases across
a network. With this option Derby runs
in the Java virtual machine (JVM) that
hosts the Server. Applications connect
to the Server from different JVMs to
access the database. The Derby Network
Server is part of the Derby software
distribution and provides this type of
framework for Derby. Derby also works
well with other, independently
developed Server applications. The
Derby documentation often refers to
this as the Network Server
configuration or client/server
configuration.
From my experience, the main differences of both modes are that in embedded mode, the database is part of your application and only one user can connect to the database.
In server mode, the database is not part of your application, runs in a different JVM or on another machine and operates in multi-user/multi-connection mode.
Regarding comparison of JavaDB and MySQL, I think it depends on your application and functional requirements. A quick google search revealed this comparison of relational db systems (Derby can be found there under letter "A" for Apache Derby).

Related

Embedded MySQL for Java

I am looking for embedded MySQL for Java. I looked at answers for "Create embedded mysql database"
and found libmysqld but it is for C++, as MySQL itself of course is written in C/C++.
Could not find better for MySQL per se.
Recalling about MariaDB, I quickly found https://github.com/vorburger/MariaDB4j but I am still raising question. Is there better solution for standalone app (that is only one local connection, we never can access that db)
UPDATE: There is no much I know. We should have embed all into one Java executable, but for the developers it should be the same as the MySQL on central server where mySQL native SQL calls are used. So having the same DB for main server and standalone app is supposed to save time developing against different DBs.

Creating database on client computer for java desktop application (swing)

I'm creating a java desktop application using swing which requires some database persistance for which I'm planning to use Hibernate. I'm pretty new to desktop application development so I wanted to know if and how the database can be created on the client computer when installed.
Is there a script to be run or maybe a hibernate configuration which initiates database creation?
A sample tutorial or example illustrating this will be ideal (although I was not able to find one).
Thanks in advance for any help provided.
Hibernate is not an database server itself, it is a object-relational mapping framework so you need a either embedded or stanalone database server. Java DB that comes with the JDK will be sufficent for desktop applications.
http://hamidseta.blogspot.in/2008/05/install-mysql-server-50-silently.html
In your package installer scripts perform the steps of silent MySql installation. In the BAT file put the code you find in the above link before triggering your Java application installer.
As per free licensing terms, one should not package MySql bundle for free, just check about the licensing if you bother.
I think it would be better if you can start with javaDB or SQLite.
They are perferred embedded database technologies for desktop java applications.
You can download the jar for sqlite-jdbc from here.
Take a look at few tutorials on JDBC
If you making swing applications, make sure you learn to use swing worker well. It will
be important to perform background processing.
Hibernate is Framework used as middle layer in project which interact between database and Business layer logic.
Hibernate features
J2EE integration
Object/Relational mapping
Automatic primary key generation
Object-oriented query language
So my suggestion is to go with MySQL because
Handles large databases. We use MySQL Server with databases that contain 50 million records. We also know of users who use MySQL Server with 60,000 tables and about 5,000,000,000 rows.
A very fast thread-based memory allocation system.
Very fast joins using an optimized one-sweep multi-join.
The server can provide error messages to clients in many languages.
This is only few features but you go to official site for more detail. And most important it's open source.
And the post help to install and configure on client system.

Getting started with Oracle Database

I've been assigned the task of making our Java web application work with Oracle Database. I am not sure where to start. I was hoping for an experience similar to working with MySQL or PostgreSQL, but no such luck.
The answer to this question will be list of steps for installing and deploying an Oracle Database for development use, basic operations such as starting and stoping, creating and dropping schemas, and perhaps even JDBC connection parameters.
A little bit of background about my project (although ideally the answer to this question will be as general purpose as possible and not be tied to the specifics of my environment):
Java (and Scala) web apps deployed in Tomcat
Hibernate (currently connected to PostgreSQL and MySQL)
Developer workstations run OS X; production deployments are to Red Hat Enterprise Linux
Firstly, don't think about Oracle on OS X. You'll need Linux (or Windows), maybe on a virtual machine/
Then decide whether you'll be using Oracle 10g or 11g. 10g has a free Express Edition which is relatively easy to install on Linux or Windows. However, if you want 11g or bug fixes to 10g or some of the extra options (compression, partitioning...) you'll need a fully licenced version of Oracle. Depends on your load, but you can have one physical machine which is licenced for Oracle, with multiple VMs (one for each developer).
XE has a single database instance that the installer will configure to start up when you start the machine. If you are just using the VM for the database, having the database startup and shutdown with the machine is the easiest way to go.
I have not found a fundamental difference between MySQL and Oracle from a developer's point of view. It's just tables, joins, and SQL. If your database gets enormous or needs insane throughput then yes, it matters a lot. But by then you'll need a real DBA.
Download SQLDeveloper from Oracle. It is free. It will let you perform database maintenance, run SQL statements interactively, etc.
Read up on "connection pooling" and "oracle". There probably some good threads here on SO. You'll want this as it will make your servets run efficiently.
Read up on JNDI database connections. This is simply a way to specify the database connection in your Tomcat configuration so your applications don't have to know anything about the database. You'll be able to move your war files from test to QA to prod with less difficulty.
Hibernate is the trickiest thing of the bunch! Keep it simple! I highly recommend you reverse engineer your model classes from the database and never manually modify them. You can also generate your DAOs (recommended) but you will modify those.
Use standard proper database design as you'll have to play fewer games with Hibernate. For example, always put a numeric PK on each table, and use an Oracle sequence to populate it. Always use FKs when appropriate. Try to normalize your data to a practical extent.
Use Ant or Maven for your builds. Don't do anything by hand.
Use SVN or similar.
That's the biggies for now.
If your database is relatively lightweight and doesn't heavily rely on Oracle-specific features, I would recommend you to use XE for development. In the other case, it might make more sense to dedicate a separate box for running Oracle for development.
As for the basic steps you mentioned, the later versions of oracle come with a rather nice web-frontend(which looks like that) that can handle all of these maintenance tasks. If you would need something better and more responsive, go for Oracle SQL Developer, which is a desktop application somewhat similar to pgAdmin.
Installation is also much easier with the newer versions of Oracle (10g and up), so you should not have any problems with that.
Edit: On memory optimization (if you insist on having an Oracle instance on your workstation and your database is relatively light), check this thread since it provides a multitude of good hints.

Embedding mysql in java desktop application

i am not clear about steps/configuration details about how i can embed mysql in a Java desktop application so that it(application) can be installed on any machine through a single executable file and doing so sets up database and also provides an exe to run the app.Till now i have built my app using netbeans and i have used mysql to set up database.plz guide me further.
MySQL isn't an embedded database - the only JAR related to it is the JDBC driver. It requires a installation process, which might be able to be scripted via Java, but the process will definitely function outside of the Java application you intend it to support. Meaning, you can turn off your Java application but the MySQL service/daemon will still be running.
Only the libmysqld is embeddable.
There are embedded databases - SQLite, Firebird - and embedded databases made in Java - HSQL, Derby/(can't remember what it was called before). I believe SQL Server Compact Edition is embeddable, while SQL Server Express/MSDE is not. I don't know if Oracle has an embeddable version....
I would strongly recommend H2. It is a very fast embedded database written in Java and I've found it easier to use than some of the others mentioned such as HSQL.
Edit:
On the H2 website, you can see a speed comparison of H2 vs Derby, HSQL, MySql, etc...
Here's information on how to backup the database.
While theoritcaly possable it would not be easy. The standard MySql distributions assume you want to set up a general purpose database server with separate from the client applications cominicating via odbc etc.
You may be better looking at the "pure java" options like HSQL or JavaDB which are designed to be embedded in a java application, and need little or no "setup".
Another possibility is Sqlite which only needs a single binary plus the sqljbbc jar file. This is again designed from scratch to be embedded inside an application and requires zero admin apart from allocating a file for the database.
Take a look at http://dev.mysql.com/doc/refman/5.0/en/connector-mxj.html. I do not remember the exact details but I was able to embed MySQL db in desktop application without user needing to install it separately.
The key class is com.mysql.management.MysqldResource.
Here is the example, http://dev.mysql.com/doc/refman/5.0/en/connector-mxj-configuration-java-object.html
The mysql-connector-mxj-gpl-db-files.jar file contains MySQL installation files for all the platforms. If you know which is your target platform, you can strip other platform versions from jar, to reduce download size for end user.
If you want an embedded database with java, then use one written in Java designed to be embedded. I know Apache Derby Can be embedded and apparently H2 too.
How big amount of data dó you need the database to handle?

How to start developing a database application using Oracle + Net Beans

I have thought of creating my first database application for one of my projects using Oracle and Java. I have chosen Netbeans as my development environment. I have a few questions to getting started.
This will be a data intensive (yet still for a college project) database application. I do not need 1000 user concurrency or any other very advanced features but basic stuff such as triggers, stored procedures etc. Will the 11g "Express" (XE) suffice for my requirements?
Do i need any Java to Oracle bridge (database connectivity driver eg. ODBC etc) for Netbeans to connect to the oracle database? If yes, what are they? Does Netbeans support Oracle databases natively?
Any easy to follow guide on how do I connect to the database and insert/retrieve/display data on a J2SE application? (I know that i should Google this but if there's any guide previously followed by anyone and is considered easy, it would be greatly appreciated.)
There are several different ways to access databases using Java. I'm assuming you are wanting to use JDBC, which is included in all recent JDK's. There are other layers on top of JDBC like Hibernate that may make things cleaner for larger applications, but may also be too steep a learning curve if you have a project to complete and submit.
To answer your questions in order:
I think it's highly likely that Oracle 10g Express Edition will do what you need for a college project. It's pretty much the 10g Standard Edition with a 4GB limit on data size.
You will need a JDBC driver to access the database from Java. It comes with XE, and is installed in <XE client install dir>/jdbc/lib/ojdbc14.jar
Sun have an introduction to JDBC here.

Categories

Resources