Im using MyBatis Generator to generate JavaBean and Mapper.xml.
But the Mapper.xml is different as I expected .
Mapper.xml contain two resultMap, BaseResultMap and ResultMapWithBLOBs.
I only want one resultMap contain all column.
Tough to know how you have your mybatis generator configuration set up, but the way to do this is through configuring the context's Model Type.
MBG provides an enum called ModelType which allows you to control how the Java Model classes (your beans) are generated. You want to use ModelType.FLAT in your context configuration to avoid any extra bean classes being created.
One option to avoid the creation of "WithBlobs" subclasses is to specify <columnOverride/> to use non-Blob jdbc types in your generator config for the Blob columns.
So, in the example of a TEXT column named "bar" in your table, you can specify VARCHAR instead:
<table tableName='foo' domainObjectName='Foo'>
<property name='useActualColumnNames' value='true' />
<columnOverride column="bar" property="bar" jdbcType="VARCHAR" />
</table>
Related
I am newbie to Liferay. I would like to build with Service Builder the following column for the entity Traveller:
<entity name="Traveller" local-service="true" remote-service="true">
<column name="citiesList" type="Collection"></column>
</entity>
When I run the service Builder, in the class TravellerModel.java the corresponding get/set methods are not generated.
I have tried the following solution without success:
<column name="citiesList" type="Collection" entity="java.lang.String"></column>
Can someone tell me how I can define a list of Strings (List) in Liferay Service Builder?
Thanks in advance.
(Closing an old thread..)
List is not a field you can simply create on Service builder by declaring it, as the rational is the same you would probably use when thinking about a DB schema, you would normally create an auxiliary table with foreign keys and values, or embed the data in a single data field to be encoded (not great).
On SB, though, you can using mapping tables or an extra entity to reformulate your list representation. Besides that you can add Lists as a parameter on you LocalServiceImpl class that consumes or build lists in a higher level.
I have tables that have this annotation structure in my application and I use JpaRepository for CRUD operations.
#Entity
#Table(name = "FOO")
public class Foo implements Serializable {
…
}
But I need to override only the table name (not any of the #Column etc. attributes) from an orm.xml or properties file without changing the actual code. I've searched and I couldn't find a way to do it in Spring Data JPA. Am I missing something here or it is not supported?
Firstly, Spring Data is not a JPA provider but simply a 'helper' library providing a wrapper round common persistence operations. The JPA specification however does provide a mechanism for overriding annotations via an XML mapping file. So assuming your provider (Hibernate, EclipseLink, OpenJPA or whatever) fully implements the JPA specification then you should be able to do this.
However it would appear that you cannot override only the table name: unless your column names are mapped to the default values then, as far as I can see, you need to specify each column in the mapping file - which is slightly inconvenient I suppose.
The book Pro JPA 2 : Mastering the Java Persistence API notes:
The metadata-complete attribute is an attribute on the entity,
mapped-superclass, and embeddable elements. If specified, all
annotations on the specified class and on any fields or properties in
the class will be ignored, and only the metadata in the mapping file
will be considered as the set of metadata for the class. When
metadata-complete is enabled, the same rules that we applied to
annotated entities will still apply when using XML-mapped entities.
For example, the identifier must be mapped, and all relationships must
be specified with their corresponding cardinality mappings inside the
entity element.
So you will need an entry in your orm.xml like the below, adding other attributes as required.
<entity-mappings>
<entity class="examples.Foo" metadata-complete="true">
<table name="NEW_FOO"/>
<attributes>
<id name="id"/>
</attributes>
</entity>
</entity-mappings>
I have 2 identical DB instances containing FOO_TABLE with the same schema. So, currently I have one class definition per DB instance:
<class name="FooTable" table="FOO_TABLE" entity-name="FooTableInstance1">
<property name="..." column="..." />
<property name="..." column="..." />
....
</class>
<class name="FooTable" table="FOO_TABLE" entity-name="FooTableInstance2">
<property name="..." column="..." />
<property name="..." column="..." />
....
</class>
The problem is that I don't want to copy-paste the properties, as the tables have the same schema. Is it possible to inherit the 2 classes from a base class which contains all the mappings and in the 2 children classes specify different entity-name?
An alternative (and perhaps the correct one if I understand your question correctly) is to use a #MappedSuperclass to define the common mappings. Whether you use this or the suggestion posted previously depends on the data model: for example are these two entities related so that you would like to be able to query across both of them?
e.g. select f from Foo returns all Foo1 and Foo2.
This cannot be done when Foo is a MappedSuperclass.
See here for further details:
http://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Mapped_Superclasses
JPA: Implementing Model Hierarchy - #MappedSuperclass vs. #Inheritance
Yes, it is possible. Take a look at the relevant documentation: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html
More specifically, check 9.1.5. Table per concrete class. Make the parent class abstract and things should work fine.
According to the documentation you need 3 Java classes:
Foo (abstract, containing all fields you want in both tables)
FooChild1 (concrete, subclass of Foo, containing no new fields)
FooChild2 (concrete, subclass of Foo, containing no new fields)
You will need two tables. One mapping to FooChild1, and another to FooChild2.
I have a legacy application that uses hibernate for mapping objects into a database. It uses the Hibernate Mapping XML file to do so. The java class contains two properties abc and def that implement java Serializable. The mapping is defined this way:
<property name="abc" column="ABC" type="serializable" length="32672"/>
<property name="def" column="DEF" type="serializable" length="32672"/>
When I try to set this up with oracle, I get a nasty error "ORA-01754: a table may contain only one column of type LONG" which essentially is complaining about creating two 'long raw' columns in one table. Oracle does not like this. After reading up on the issue, the recommended approach is to use blobs instead of 'long raw' types.
My question is, how can I express in the hibernate mapping file to use a serializable type mapped into a blob column? I would think there would be a serializable_blob type but there does not seem to be.
I know this is possible with JPA annotations using #Basic and #Lob. It should also be possible using the hibernate mapping file. How can this be done in the hibernate mapping file?
Update:
The following do not work as Serializable works:
type=binary - This one expects a byte[]. Does not work for Serializable classes. Gives ClassCastException.
type=blob - - This one expects a java.sql.Blob. Does not work for Serializable classes. Gives ClassCastException.
type=materialized_blob - - This one expects a byte[]. Does not work for Serializable classes. Gives ClassCastException.
<property
name="data"
type="blob"
column="DATA"/>
...
should work.
Ok, did some more research following my comment above and, by Jove, I found it.
In Hibernate 3.5 + Spring 3.1, I used Spring's org.springframework.orm.hibernate3.support.BlobSerializableType. Now I'm upgrading to Hibernate 4.3, that option isn't available anymore. I did find the type to column mappings as OP, but in my application there are various Strings (legacy) that are mapped to BLOB fields.
So, as I reported in the comment above, I found the org.hibernate.type.SerializableToBlob type, which is parameterize. Below how I got the mapping to work (using good old-fashioned hbm.xml mappings)
<property name="description" column="TEXT">
<type name="org.hibernate.type.SerializableToBlobType">
<param name="classname">java.lang.String</param>
</type>
</property>
And that appears to do the trick. (the classname value should be the type of the attribute you are mapping, I think)
I am having a querying issue in Hibernate. I have a table, 'test', with existing data. I have a requirement where I can not modify the schema of the test table, so I created another table, 'testExtension', whose primary key is a foreign key to the primary key of Test. Data in testExtension is a subset of the data in test. i.e. There will be less rows in 'testExtension' than in 'test'.
I have defined this relationship in a configuration file as follows:
<class name="Test" table="test">
<id name="testId" column="test_id">
<generator class="sequence">
<param name="sequence">test_id_seq</param>
</generator>
</id>
<property name="name"/>
<joined-subclass name="TestExtension" table="testExtension">
<key column="test_id"/>
<property name="summary" />
<property name="homepage"/>
</joined-subclass>
With this setup, I am able to create a TestExtension object in my Java program, populate it with data, 'save' it via Hibernate, and commit the transaction. And it correctly saves data in both Test and TestExtension.
My problem is occurring when I am trying to query data from these tables. Right now if I query for a particular test_id using the TestExtension.class to QBE, it will only return a row if that id exists in both Test and TestExtension. If I use the Test.class to QBE, it will return the row but I will not have access to any of the data stored in TestExtension.
My question is: how can I query these tables so that the results are based off a 'left outer join' of both Test and TestExtension? Any solution is appreciated, whether it's query by example, HQL, or something else (though preferably not raw SQL).
Thanks!
HQL is probably the easiest way to do this. Docs are here:
http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql-joins.html
Sounds like what you might want to do is remap your relationships so that Test and TestExtension use a one-to-one relationship instead of inheritance. Then you can query for Test and TestExtension using a left outer join across the one-to-one.
If you use HQL to write a query for the Test class, it should do what you want. I assume QBE is effectively adding the class of your example entity as one of the query parameters.
So sth like:
from Test t where t.property = :value
should return either Test or TestExtension entities. Note that (at least with the versions of Hibernate I've used). In this case, Hibernate should immediately give you back the actual entities rather than a proxy too--- be aware that TestExtension entities can sometimes be returned as plain Test lazy-loading proxies.