Friday July 25, 2008
TOTD #38: Creating a MySQL Persistence Unit using NetBeans IDE
This TOTD (Tip
Of The Day) shows how to
create a Persistence Unit (PU) for a MySQL
database using NetBeans
IDE. This PU can then be used in any of Java EE artifacts (JSP,
Servlet, EJB, ...) for database interaction.

| ~ >sudo
mysqld_safe --user root Password:<YOUR PASSWORD> Starting mysqld daemon with databases from /usr/local/mysql/data |
| mysql> CREATE
USER duke IDENTIFIED by 'duke'; Query OK, 0 rows affected (0.00 sec) mysql> create database states; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL on states.* TO duke; Query OK, 0 rows affected (0.00 sec) |


| CREATE TABLE STATES ( id INT, abbrev VARCHAR(2), name VARCHAR(50), PRIMARY KEY (id) ); |

| INSERT INTO STATES VALUES (1, "AL", "Alabama"); INSERT INTO STATES VALUES (2, "AK", "Alaska"); INSERT INTO STATES VALUES (3, "AZ", "Arizona"); INSERT INTO STATES VALUES (4, "AR", "Arkansas"); INSERT INTO STATES VALUES (5, "CA", "California"); INSERT INTO STATES VALUES (6, "CO", "Colorado"); INSERT INTO STATES VALUES (7, "CT", "Connecticut"); INSERT INTO STATES VALUES (8, "DE", "Delaware"); INSERT INTO STATES VALUES (9, "GL", "Florida"); INSERT INTO STATES VALUES (10, "GA", "Georgia"); INSERT INTO STATES VALUES (11, "HI", "Hawaii"); INSERT INTO STATES VALUES (12, "ID", "Idaho"); INSERT INTO STATES VALUES (13, "IL", "Illinois"); INSERT INTO STATES VALUES (14, "IN", "Indiana"); INSERT INTO STATES VALUES (15, "IA", "Iowa"); INSERT INTO STATES VALUES (16, "KS", "Kansas"); INSERT INTO STATES VALUES (17, "KY", "Kentucky"); INSERT INTO STATES VALUES (18, "LA", "Louisiana"); INSERT INTO STATES VALUES (19, "ME", "Maine"); INSERT INTO STATES VALUES (20, "MD", "Maryland"); INSERT INTO STATES VALUES (21, "MA", "Massachussetts"); INSERT INTO STATES VALUES (22, "MI", "Michigan"); INSERT INTO STATES VALUES (23, "MN", "Minnesota"); INSERT INTO STATES VALUES (24, "MS", "Mississippi"); INSERT INTO STATES VALUES (25, "MO", "Missouri"); INSERT INTO STATES VALUES (26, "MT", "Montana"); INSERT INTO STATES VALUES (27, "NE", "Nebraska"); INSERT INTO STATES VALUES (28, "NV", "Nevada"); INSERT INTO STATES VALUES (29, "NH", "New Hampshire"); INSERT INTO STATES VALUES (30, "NJ", "New Jersey"); INSERT INTO STATES VALUES (31, "NM", "New Mexico"); INSERT INTO STATES VALUES (32, "NY", "New York"); INSERT INTO STATES VALUES (33, "NC", "North Carolina"); INSERT INTO STATES VALUES (34, "ND", "North Dakota"); INSERT INTO STATES VALUES (35, "OH", "Ohio"); INSERT INTO STATES VALUES (36, "OK", "Oklahoma"); INSERT INTO STATES VALUES (37, "OR", "Orgeon"); INSERT INTO STATES VALUES (38, "PA", "Pennsylvania"); INSERT INTO STATES VALUES (39, "RI", "Rhode Island"); INSERT INTO STATES VALUES (40, "SC", "South Carolina"); INSERT INTO STATES VALUES (41, "SD", "South Dakota"); INSERT INTO STATES VALUES (42, "TN", "Tennessee"); INSERT INTO STATES VALUES (43, "TX", "Texas"); INSERT INTO STATES VALUES (44, "UT", "Utah"); INSERT INTO STATES VALUES (45, "VT", "Vermont"); INSERT INTO STATES VALUES (46, "VA", "Virginia"); INSERT INTO STATES VALUES (47, "WA", "Washington"); INSERT INTO STATES VALUES (48, "WV", "West Virignia"); INSERT INTO STATES VALUES (49, "WI", "Wisconsin"); INSERT INTO STATES VALUES (50, "WY", "Wyoming"); |



|
<properties> <property name="toplink.jdbc.user" value="duke"/> <property name="toplink.jdbc.password" value="duke"/> </properties> |
| <?xml version="1.0"
encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="AutocompletePU" transaction-type="JTA"> <jta-data-source>jndi/states</jta-data-source> <class>server.States</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="toplink.jdbc.user" value="duke"/> <property name="toplink.jdbc.password" value="duke"/> </properties> </persistence-unit> </persistence> |
| EntityManager em; @Override public void init() throws ServletException { EntityManagerFactory emf = Persistence.createEntityManagerFactory("AutocompletePU"); em = emf.createEntityManager(); } |
|
@PersistenceContext(unitName="AutocompletePU") EntityManager em; |
|
String abbrev = request.getParameter("abbrev"); List<States> list = em.createNamedQuery("States.findByAbbrev"). setParameter("abbrev", abbrev). getResultList(); if (list.size() > 0) { States s = list.get(0); out.println("Found " + s.getName() + " with abbrev \"" + abbrev + "\""); } else { out.println("No matching state found with \"" + abbrev + "\""); } |


Posted by Arun Gupta in web2.0 | Comments[15]
|
|
|
|
|
Today's Page Hits: 507
Total # blog entries: 1009
Do you have to have the userid/password in the persistence.xml file? What happens if the userid/password changes from environment to environment (i.e. development to production)? Cannot we use a jndi datasource and not worry about the connection details? How would that look in the persistence.xml?
Posted by Jason Kilgrow on July 28, 2008 at 04:51 AM PDT #
Jason, http://davidwburns.wordpress.com/2008/03/13/how-to-use-a-jndi-datasource-with-jpa-in-netbeans-601/ provides some more details on the topic of your interest.
Posted by Arun Gupta on July 28, 2008 at 10:43 AM PDT #
Never mind, I just looked at the
servlet code again. The list created is
based on the findByAbbrev query...
Sorry.
John
Posted by John Gregory on July 29, 2008 at 02:50 AM PDT #
Posted by Arun Gupta's Blog on July 29, 2008 at 09:30 AM PDT #
This is fine!
Posted by 62.38.218.180 on August 18, 2008 at 10:30 PM PDT #
Posted by Arun Gupta's Blog on August 20, 2008 at 06:20 AM PDT #
I have created a new table using the execute command utility for a PostgreSQL data base. The table got created in PostgreSQL data base.
But it is not being viewed in Databases -> Mydatabase-> tables under Services tab.Due to this i am not able to create the persistence unit.please help
Posted by Karthik on August 21, 2008 at 04:44 AM PDT #
Karthik,
I've not tried PostgreSQL within NetBeans. However I found the blog entry at:
http://blogs.sun.com/phantom/entry/solaris_postgresql_and_netbeans_perfect
that describes a similar setup. Otherwise, please post your question to nbusers@netbeans.org for
Archives of the alias are available at:
http://www.netbeans.org/servlets/SummarizeList?listName=nbusers
Posted by Arun Gupta on August 21, 2008 at 03:57 PM PDT #
I'm having a little trouble with this example. On this line of code:
States s = list.get(0);
I'm getting a ClassCastException. I'm wondering if somehow I'm using two different class loaders. Does anyone have an idea what I could be doing wrong?
Thanks!
Posted by Carol on October 08, 2008 at 01:48 PM PDT #
Carol, I've seen it sometimes. The error disappeared if I re-create the project following exactly the same steps of process.
Posted by Arun Gupta on October 08, 2008 at 05:35 PM PDT #
Posted by Arun Gupta's Blog on June 24, 2009 at 08:57 PM PDT #
Posted by Arun Gupta's Blog on June 24, 2009 at 09:01 PM PDT #
Posted by Arun Gupta's Blog on August 05, 2009 at 06:14 AM PDT #
Posted by Arun Gupta's Blog on August 13, 2009 at 05:42 AM PDT #
Posted by Arun Gupta's Blog on August 13, 2009 at 10:58 AM PDT #