I am making a cloud like project. Right now, what I'm going to have is that I get the downloadable folder for the user to install. When users drag and drop files into the folder they are added to an XML file that looks like this:
<File>
<Name>application_for_employment2.doc</Name>
<Type>doc</Type>
<Size>144384</Size>
<Path>\application_for_employment2.doc</Path>
<Last_Mod>1365992658437</Last_Mod>
</File>
My questions:
What is the best way to read this and get the info?
Do I need to store this into my database?
Use JDOM for Parsing XML and JDBC connection to persist data.
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("File");
For JDOM CODE Refer Exmaple link
XStream to get the XML into Beans and then Hibernate to put it into the database. Once your beans are set up and Hibernate is configured, the code is trivial:
Bean xml = (Bean)new XStream().fromXml(xmlStream);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
xml.setId(session.save(xml));
session.commit();
You can use JAXB (JSR-222) to read the XML into Java objects (an implementation is included in Java SE 6).
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
If you need to store the data to a database the. You could use the JPA APIs (JSR-327, with EclipseLink, Hibernate, OpenJPA, as the underlying implementation) to persist those same Java objects.
http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-25.html
There are many ways of doing this. I would suggest using StAX for reading the XML and JDBC to store info in database.
Related
I have a central xml configuration in an Commons project. This means in this project I don´t know which xml elements in the config exists. I want to hold the root element in the configuration. The requestet configuration item from other project has to be with a class object. This class object contains neccessary information of the requestet xml Element with the jaxb annotation. Is it possible to get a filled object from JAXB with this guidelines?
I have no code examples because I don´t know yet how to start.
JAXB mapps the XML structure into Object oriented graph/hierarchy.
If you don't know the structure/hierarchy of XML, you should use push/pull parsing SAX, StAX, ...
I have wrote some POJOs in NetBeans, and want to map these entities automatically to an empty database, to be tables.
I have read the netbeans official tutorial https://netbeans.org/kb/docs/java/hibernate-java-se.html#06a
But using Hibernate Mapping File as the document says can not choose the Database Table value, compare to the pic
(https://netbeans.org/images_www/articles/70/java/hibernate-j2se/mapping-wizard.png,
The actor value will not show since my database is empty.
So what should I do if I followed the tutorial, or is there any other method to automatically create tables by POJO in NetBeans?
There is one property hibernate.hbm2ddl.auto in hibernate which create tables as per your pojo structure.
Refer this doc.
MyBatis Generator can generate POJOs of tables. Visit this site to help you get started http://code.google.com/p/mybatis/wiki/Generator
Follow this tutorial on Hibernate mapping files.
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/xml.html
I never used NetBeans but if you write mapping file by yourself then you can use an empty database.
Use SchemaExport.export. Run following code in main() method:
AnnotationConfiguration configuration = new AnnotationConfiguration();
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.export(true, true, true, false);
I am using struts2 with hibernate. Does anyone know if it is possible to return query result as XML instead of ArrayList of domain objects?
Hibernate by default maps and persists a database record thought POJO , but in fact it also supports persisting , mapping and representing a database record in XML by using an experimental features called Dynamic models.
For example , to output a record in XML:
/**Get the a new session that is in the DOM4J EntityMode**/
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element outputXML=(Element) dom4jSession.get(Employee.class, employeeId);
XMLWriter writer = new XMLWriter( System.out, OutputFormat.createPrettyPrint() );
writer.write( outputXML);
To configure the format of the outputted XML , you can only do it by mapping the entity in XML . AFAIK ,there are no annotation equivalent .
Hibernate is an Object-Relational Mapper, meaning it maps a Relational database to objects. You want to use Hibernate to return an object and then use an XML Serializer to convert to XML.
The Simple Serializer is probably the best one to get started with. The Website contains a lot of tutorials and examples.
http://simple.sourceforge.net/
However there are a ton of XML Serializers for Java:
http://karussell.wordpress.com/2009/09/03/xml-serializers-for-java/
Maybe you could, once you have the result use XStream to parse the entire result to XML. A simple tutorial on XStream is available here.
While working on some java projects i've saw some sort of SQL repository.
The idea was to place all queries in one(or few) xml files and retrieve them when needed by name. Something like this:
String sql = getSQLRepository().getSQL("SELECT_ALL_ROWS", params)
String sql2 = getSQLRepository().getSQL("SELECT_ROWS_WITH_COND", params)
In my current Grails project i have a lot of HQL queries in dozens of classes and it's hard to track them all. It seems that HQL repository would be very nice solution.
So could anyone tell if some sort of SQL\HQL repository implementation allready present or there are better solutions present ?
Have a look at Mapping Queries in Hibernate reference.
After we started use the http://source.mysema.com/display/querydsl there is no need to think about text queries and how to manage them.
I'd recommend you to use the good old properties files. You can put them into your classpath and then use as following:
Properties sql = new Properties();
properties.load(getClass().getResourceAsStream("sql.properties"));
////////
String query = sql.get("SELECT_ALL_ROWS");
I'm sorry, and it doesn't relate to Hibernate, but when I worked with iBatis, - there are the situation as you are writing about exactly. A few xml (partially generated itself) files, containing SQL queries, which was easily to use in DAO
I have set up Hibernate Tools from within Eclipse to autogenerate classes based on an existing DB. For each of the tables I have documented them and each of their columns within SQL Server. Is there a way to use that documentation information to comment the generated classes and to populate the schema entity documentation? I see that there are meta tags that can be put in the hbm.xml mapping files, but since I have those autogenerated each time I'd need to either add them back in or continually merge in new changes, plus I'd ideally like to have the DB be the "truth" information and not store this sort of information in the mapping files. Does anyone know if this is possible and if so how to do it? Thanks...
Is there a way to use that documentation information to comment the generated classes and to populate the schema entity documentation?
To my knowledge, tables and columns comments are used in some of the the generated files, at least in the following templates from hibernate-tools.jar:
doc/tables/table.ftl
hbm/column.hbm.ftl
hbm/persistentclass.hbm.ftl
For example, in hbm/column.hbm.ftl:
<#if column.isFormula()>
<formula>${column.getFormula()}</formula>
<#else>
<column name="${column.quotedName}" ${c2h.columnAttributes(column)}<#if column.comment?exists && column.comment?trim?length!=0>>
<comment>${column.comment}</comment>
</column><#else>/>
</#if>
</#if>
But they aren't used in the templates for annotated POJOs, you'd have to modify the templates for this.
If you use Hibernate tool task to generate pojo classes out of HBM files and Database,It will add documentation in the generated java classes by default.You can see it in pojoTypeDeclaration ftl file.
/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
*/
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}
Where pojo.getClassJavaDoc will generate documentation if your hbm file has meta attribute declared such as CLASS_DESCRIPTION.