hibernate version property mapping in reverse engineering file - java

I'm working on application that uses 66 tables (SQL Server 2012). Already, hibernate refuses to recognise the "identity" property set for each id column, so I have to put in the <table> element below in my reverse engineering to fix that, just in case I have to re-map my table after making changes to them in SQL Server 2012:
<table name="EMPLOYEE">
<primary-key>
<generator class="identity"/>
</primary-key>
</table>
The question is, I want to do the same for the version property without having to go into all 66 hbm.xml files to add <version name=”version” type=”long” /> every time I map my tables.
Is there a way to set something in the reverse engineering or somewhere else so that hibernate automatically adds the version property?
Thanks

Figured this out. The answer is to ensure that your versioning column in your db is called "version" and hibernate will automatically set the versioning in the xml mapping for you.

Related

How to create an index in Liquibase with a column from other table?

I want to create an index for an specific sql call in which I join two tables and I don't know how to insert the outer column.
This is what I currently have:
<changeSet id="1234" author="name" >
<createIndex catalogName="catalog"
indexName="idx-master"
tableName="table-a">
<column name="type"/>
<column name="id"/>
<column name="date"/>
</createIndex>
</changeSet>
What I'm looking for is something like a tag column in which I can pass the name of the outer column, so it could be like:
<changeSet id="1234" author="name" >
<createIndex catalogName="catalog"
indexName="idx-master"
tableName="table-a">
<column name="type"/>
<column name="id"/>
<column name="date"/>
<column name="date-from-table-b">
</createIndex>
</changeSet>
Thanks
Note that an index over different tables is likely a database system specific functionality (I'm not aware of an RDBMS that supports this, but some may). Therefore a) it's unlikely that liquibase supports it with general functionality and b) if you add a custom SQL statement to create that index, your liquibase specification won't be database system independent anymore.
The latter may not be one of the reasons you use liquibase, but I'd still give a redesign of the table structure a thought. This indicates a design issue. Sometimes it's worthwhile to create a separate table for such special lookups or to store the data joined in general.
The closest you would get with general functionality is to create your index as you already wrote it down in the question and have an additional one-column index on the date in your joined table.
I don't think it's possible with special liqubiase tags, but you can always use <sql> tag and create it in plain SQL.
As such:
<changeSet id="foo" author="bar">
<sql>
CREATE INDEX some_index ON ...
</sql>
</changeSet>

How to know if lazy loading worked via hibernate mapping?

I have a Person object with 5 columns and i used following properties:
property name="firstName" column="FirstName" type="string" lazy="false"
property name="lastName" column="LastName" type="string" lazy="true"
property name="city" column="City" type="string" lazy="true"
property name="country" column="Country" type="string" lazy="true"
property name="phone" column="Phone" type="string" lazy="true"
Now how do i find out if the object i get back using "From Person" contains only rows of data with the FIRSTNAME within it - assuming the other 4 are lazy loaded. I tried debugger but when i dive in i see all values has already been there... is there something missing? thanks
This might be helpful :When asked to load a lazy field, Hibernate loads all lazy fields
But, I thing it will not improve performance significantly, if it is a goal, unless you have hundreds of columns in the table. Leazy loading makes a big difference when featching collections.
To find out if it's lazy loaded or not without checking the generated sqls you can simply close the session and then access the property.
However, you need further bytecode enhancement to enable this type of behavior. http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html_single/#performance-fetching-lazyproperties
For instance, after enhancing the bytecode to enable lazy loading of horse (string property) inside Note and running.
Criteria criteria = session.createCriteria(Note.class);
List<Note> list1 = criteria.list();
session.close();
String horse = list1.get(0).getHorse();
I get
Caused by: org.hibernate.LazyInitializationException: Unable to perform requested lazy initialization [stackoverflow.Note.horse] - no session and settings disallow loading outside the Session
at org.hibernate.bytecode.enhance.spi.interceptor.Helper.throwLazyInitializationException(Helper.java:165)
You can use the config hibernate.show_sql=true and view the output SQL.

How to index existing documents in java with Solr

I am trying to write an indexer for a search engine in Solr, using Java. I've been googling a lot. I found different approaches such as using a Core Container which adds the document and then the Solr server indexes all the data. another approach is to use Nutch, using Solr Indexer.
I am new with Solr and do not know which code to use. BTW, I need to have the stored indexed document. I do not know where Solr is saving the indexed documents.
BTW, would it be better to use Nutch?
I am so new with Solr, so any help would be appreciated.
You can use the Solr Data Import Handler.
Basically, in the core you create a data-config.xml with the datasource
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="db_username" password="db_password"/>
and then you define how should Solr map the database information into Solr indexes something like:
<document name="products">
<entity name="item" query="select * from item">
<field column="ID" name="id" />
</entity>
</document>
More reference here.
Also, if you need to use Java code, you can try with spring-data-solr.
Hope this helps!

How to store textarea input into a database with hibernate?

I made a textarea in a jsp file called information.jsp and im trying to get it to store the input from it into a database when the user clicks submit. I've made the information.hbm.xml file as shown below:
information.jsp:
<textarea id = "desc" rows="5" cols="115" onkeypress="textCounter(this,20);"><c:out value="${informationView.storedDescription}"/></textarea>
information.hbm.xml
<hibernate-mapping>
<class name="Information" table="INFO_USER">
<id name="id" type="long" column="INFO_ID">
<generator class="native">
<param name="sequence">ID_SEQ</param>
</generator>
</id>
<property name="description" column="DESC"/>
</class>
</hibernate-mapping>
I then made a class Information with getters and setters for description to store and retrieve the information from the database. I just cant figure out how to get the input into description from the textarea from a submit event...
From what I've been reading I think I have to make an InformationAction to actually get it to save when someone clicks submit but again not sure. I'm new to Hibernate and a little lost on where I went wrong in the process of saving the input to the database and retrieving it to load into the textarea automatically if someone reopens the page.
I just can't figure out how I'm going to pass the input from the textarea to the database.
Any help would be great since I've been working on this for a long time and can't figure it out. Let me know if you need more info, thanks.
Yes you will need either InformationAction or InformationContoller depending on the web framework you want to use. Your action or controller needs to have a description property that maps to the text area value. If you use web frameworks like Struts2 or Spring MVC, this is pretty easy to achieve.
Now coming to hibernate part. Your action needs to have hibernate Session object that can read and write values to the database. You can then construct your Information object using the description you got from front end and then call saveOrUpdate() method on session.
The code would be something like this
public class InformationAction {
//Maps to text area value. Needs getter and setter
private String description;
//Inject session from hibernate configuration
private Session session;
public void someMethod() {
Information information = new Information();
information.setDescription(description);
session.saveOrUpdate(information);
}
}
This will save a row in your Information table.

Hibernate: How do I join a table with itself?

I have a problem joining a table. Actually I'm really lost on how to do it. I have this Hibernate table mapping.
<class name="Technology" table="TECHNOLOGIES">
<id name="technoId">
<column name="techno_id" />
<generator class="identity" />
</id>
<property name="description" type="java.lang.String">
<column name="description" />
</property>
<many-to-one name="parent" class="Technology" />
</class>
The column parent is related with the same table. An example of the table so you can understand me.
techno_id | description | parent
1------------"Java"----------null
2------------"Hibernate"------1
3------------"HQL"------------2
4------------".NET"----------null
5------------"NHibernate"----4
That's basically an example of the table, it doesn't have real data, it's just an example.
What I want to do is, in a method that receives a techno_id, run with Criteria or HQL, and that brings me a List of results with the children id's too.
For example, if I send to the method the "1" techno_id, it should bring me a List with the ids "1, 2, 3".
I hope I was as clear as possible and you can help me.
Thanks, and sorry for my english jaja.
You need to use join tag with inverse
<join table ="Technologies" inverse ="true" optional = "false">
<key column = "techno_id"/>
<many-to-one name="parent" class="Technology" />
</join>
There are two ways of doing it.
By Criteria :
1. For specific level fetching : e.g 3 level fetching(As per your example)
public Technology getAllChildrenTechnology(long parentID){
Crtieria criteria = session.createCriteria(Technology.class);
criteria.add(Restriction.eq("id",parentID));
criteria.setFetchMode("parent",FetchMode.JOIN);
criteria.setFetchMode("parent.parent",FetchMode.JOIN);
criteria.setResultTransformer(criteria.DISTINCT_ROOT_PROPERTY);
Technology techno = (Technology)criteria.uniqueResult();
return techno;
}
2. For N level Fetching : You need to create n level for loop.(Recursive loop using above).
By Lazy :
There is a feature in hibernate that allow us to fetch some joins without using criteria.
<many-to-one name="parent" class="Technology" lazy="false"/>
I believe this question is not really Hibernate specific. Even you are writing SQL, you have no easy way to get a node with all its descendant, without special handling in design.
Easiest way is to have each Technology contains List of child technologies (Bidirectional relationship). Then traverse Technology's children recursively.
If you need quick retrieval from persistence storage, you have to cater that in your design. One way is as described in an answer I wrote before for a similar question. https://stackoverflow.com/a/7524077/395202
This method is not that Hibernate friendly anyway (It still works, just need extra attentions)

Categories

Resources