2

Let's say I have a Java object that with variables x, y, and z and I would like to automatically import it into a MySQL database table with columns id, x, y, and z. Is there an easy way I can accomplish that or do I have to do it manually for each separate class?

Thanks.

Edit: By "automatically", it'd be like I can do something like:

some_instance.importIntoDB(database_table_name)

or

some_instance = extractFromDB(database_table_name, conditions);

Obviously, it wouldn't be that simple, but I am looking for something that's streamlined similar to object serialization and turning java objects into XML and back.

2
  • What do you mean by "automatically"? Where do the values come from?
    – Keppil
    Aug 9, 2012 at 14:51
  • Thanks for the keyword, looking into J2EE right now. And to whoever downvoting this, please note that not everyone specialized in software engineering at school. Aug 9, 2012 at 15:18

1 Answer 1

2

Answering my own question, since others considered that to be too trivial to be bothered with more than a keyword answer:

Java EE has a package (persistence) that specifically deals with linking Java objects directly with MySQL tables.

1. Making a persistence.xml

A core component of this approach is to define a persistence.xml file that specifies the various properties associated with the Java-MySQL mapping. Typically a Java IDE (i.e. Netbeans, Eclipse) will have an interface that deals with making an appropriate persistence.xml.

2. EntityManager object

The EntityManager in Java EE is the key controller class that deals with the object mapping. It can be generated by

EntityManager em = Persistence.createEntityManagerFactory(
        [PERSISTENCE_UNIT]).createEntityManager();

where [PERSISTENCE_UNIT] is the "persistence unit name" specified in creating the "persistence.xml" file (through an IDE interface)

Examples of how to use EntityManager can be found here: http://docs.oracle.com/javaee/6/tutorial/doc/gijst.html

3. Markup the object classes

The datatypes that need to be mapped to a database needed to be marked up with annotations that start with @, to specify how they are used. An example can be found in the following link: http://hop2croft.wordpress.com/2011/07/06/jpa-basic-example-with-entitymanager-spring-and-maven/

Note: The author of this link uses Maven and Spring. If Netbeans is being used, most of the page can be ignored and the only relevant part is in the part that define the "Car" class (just search for "public class Car"), where it offers an educational example of how to mark up a class.

Note2: Since the author uses Spring, a lot of the annotations that appear outside of "Car.java", such as @Transactional, may not apply.

One possible problem a user can encounter in the process is the treatment of primary keys, which are often set to "auto increment". This page addresses the issue of having the system to automatically handle primary keys in the event that a new object instance is to be inserted into the database: http://www.developerscrappad.com/408/java/java-ee/ejb3-jpa-3-ways-of-generating-primary-key-through-generatedvalue/

Lastly, it is a good idea to add all the classes/objects that have the @Entity markup to the persistence.xml as it may cause runtime issues if this is not done.

Hope this helps other newbies who may otherwise get downvoted for asking the same apparently ignorant question that I just asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.