Eclipselink auto creates utf8_unicode_ci - java

When I start my application, eclipselink is set to auto drop and create the table in the database. I have set it up to create an InnoDB engine using the table creation suffix. However, I would like the text encoding to be utf8_unicode_ci. How can I set up my xml file to achieve that. Right now, it defaults to utf8_general_ci.
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSc...>
<persistence-unit name="Data">
<class>com.core.objects.StandardObject</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/data" />
<property name="javax.persistence.jdbc.user" value="username" />
<property name="javax.persistence.jdbc.password" value="password" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.ddl-generation.table-creation-suffix" value="engine=InnoDB " />
</properties>
</persistence-unit>
</persistence>

It's been a while since you have asked this question, anyway I am posting this answer by chance if any one wants to know.
Change your "javax.persistence.jdbc.url" properties value from
"jdbc:mysql://localhost:3306/data"
to
"jdbc:mysql://localhost:3306/data?useUnicode=yes&characterEncoding=UTF-8".
This will enable unicode support for your database.
Hope you had figured it by now ;)

when auto create table you can config this parameter:
<prop key=eclipselink.ddl-generation.table-creation-suffix>ENGINE=InnoDB DEFAULT CHARSET=utf8</prop>

Related

Check For Unmapped Columns In Hibernate Mapping

I'm writing a database synchronization tool for existing databases. One of the requirement is to validate the entities with the database schema. The program has to be aware of any database changes (like adding new columns to the database). I'm planning on using Hibernate for this project.
Is there anyway Hibernate can validate the difference between the entity and the table in the database?
Can Hibernate output SQL script so I can review the SQL script before running it to update the target database?
Yes hibernate can output the SQL in the logs.
You need to set the : - <property name="hibernate.show_sql" value="true"/>
but SQL generated by the server is little messy so you have to modify the column name as per your database table.
please have a look at the following XML of my project with the hibernate
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="communityPortalPersitenceUnit">
<class>com.googlecode.frameworksintegration.domain.Blog</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/conhint_prod" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
<property name="hibernate.show_sql" value="true"/>
<!--Start :- connection pooling -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />
<!--End :- connection pooling -->
</properties>
</persistence-unit>
</persistence>

Exclude one class from creating a Table in hibernate

I have a persistence unit like this:
<persistence-unit name="persistence Unit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.User</class>
<class>com.example.Team</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
<!-- JTA stuff -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.BTMTransactionManagerLookup" />
</properties>
</persistence-unit>
When the server loads first time, this creates table for User and Team, but i dont want to create a table for Team. Is it possible?
Some settings in the persistence table maybe?
If table Team isn't changed (INSERT or UPDATE) by your application in any way, you could remove write permissions for it in your database system.
Exclude Team class from your persistance.xml Remove following line.
<class>com.example.Team</class>

Unable to create OpenJPAEntityManagerFactory

I have created the OSGI bundle project with JPA 2.0 support. I am using OpenJpa as a JPA provider. when I run the bundle, I Could not create OpenJPAEntityManagerFactory. Please fine the code which I used to create OpenJPAEntityManagerFactory.
OpenJPAEntityManagerFactory emf = OpenJPAPersistence.createEntityManagerFactory(
"StudentServiceProvider",
"META-INF/persistence.xml");
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="StudentServiceProvider">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.student.serviceprovider.model.Student</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/NYL" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="root" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
<!-- To avoid foreign key issues -->
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)" />
<!-- Auto create tables -->
<property name="jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
</persistence>
In src folder I have META-INF and i created the persistence.xml with in that(META-INF/persistennce.xml).
Please let me know to resolve this issue.
It'd probably be helpful if you listed what the error is you are getting, but it might be because you have this line twice:
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
This is just a stab in the dark since you don't mention what the error is and I just happen to be getting an error message of "Equivalent property keys "openjpa.Log" and "Log" are specified in configuration." for something I'm working on.

SSL with EclipseLink JPA

Is it possible to make JPA use SSL for it's connections. I don't know very much about JPA, so I can't provide many details about what version and provider I'm using, but here is a stripped version of my persistence.xml file. Is there a property you can add that makes it use secure connections?
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="MY_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL">
<!-- declare class entities here -->
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://URL_TO_SERVER" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="USERNAME" />
<property name="javax.persistence.jdbc.password" value="PASSWORD" />
<!-- Optimize database writes to use batching. -->
<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
<!-- Avoids flush being triggered before every query execution. -->
<property name="eclipselink.persistence-context.flush-mode"
value="COMMIT" />
<!-- Configure connection pool. -->
<property name="eclipselink.jdbc.connections.initial" value="1" />
<property name="eclipselink.jdbc.connections.min" value="64" />
<property name="eclipselink.jdbc.connections.max" value="64" />
<!-- Timeout for connection. -->
<property name="eclipselink.jdbc.timeout" value="10" />
<!-- Configure cache size. -->
<property name="eclipselink.cache.size.default" value="1000" />
<!-- Configure database to be created on startup if not already existing.-->
<property name="eclipselink.ddl-generation" value="create-tables" />
<!-- Configure simple SQL logging for demonstration. -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="false" />
<property name="eclipselink.logging.timestamp" value="false" />
</properties>
</persistence-unit>
It is not anything JPA specific, just about adding arguments to JDBC connection String. Assuming that everything else is correctly set, then adding this should be enough:
?useSSL=true&requireSSL=true
If SSL connection in general is not working, then this page provides more information: MySQL 20.3.4.5. Connecting Securely Using SSL
I found this was useful in EclipseLink 2.5.0 to pass properties through to the JDBC driver:
<property name="eclipselink.jdbc.property.my_property_name" value="my_value" />
This is driver specific, but in your case it would be:
<property name="eclipselink.jdbc.property.useSSL" value="true" />
<property name="eclipselink.jdbc.property.requireSSL" value="true" />

EclipseLink into OSGI Bundle persistence.xml not found

I'm having some problem with EclipseLink. I'm using GlassFish v3.1 and I'm trying to use EclipseLink for my persistence layer. I followed all tutorials available on the Eclipse wiki without luck. My persistence.xml file cannot be parsed and I receive this error while trying to create the EntityManagerFactory:
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: bundle://307.1:1/
Here is my persistence.xml located in /WEB-INF/classes/META-INF/:
<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="generic">
<class>com.generic.domain.Service</class>
<properties>
<!-- Embedded MySQL Login -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"/>
<!-- TODO: replace with connection pool -->
<property name="javax.persistence.jdbc.userid" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.jdbc.read-connections.min" value="1"/>
<property name="eclipselink.jdbc.write-connections.min" value="1"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<!-- Logging Settings -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.logging.timestamp" value="false"/>
</properties>
</persistence-unit>
</persistence>
I added this line to my MANIFEST.MF:
JPA-PersistenceUnits: generic
I can now confirm that it's a bug in EclipseLink. The work around to your problem is to either get hold of EntityManagerFactory using JNDI lookup or #PersistenceUnit instead of doing Persistence.createEntityManagerFactory().
1) It looks like you are trying to use OSGi/JPA from your WAB. Correct?
2) Can you tell if you have installed any new eclipselink bundles in your system? If so, what are they?
3) Can you provide stack trace?

Categories

Resources