I am trying to map a MySql view to hibernate. But when I am getting and Table missing error while starting the application.
view is
CREATE
ALGORITHM = UNDEFINED
DEFINER = `mydbadmin`#`%`
SQL SECURITY DEFINER
VIEW `Reservation_Transaction_view` AS
select
convert( concat(`apst`.`Tid`,
_utf8'-',
`apst`.`RoomIndex`,
_utf8'-',
`apst`.`Rid`) using utf8) AS `id`,
`apst`.`Tid` AS `transactionId`,
`apst`.`RoomIndex` AS `room no`,
`apst`.`Pid` AS `paymentId`,
`apst`.`Rid` AS `reservationId`
from
`agent_payment_sub_transaction` `apst`
where
(`apst`.`Rid` <> 0)
group by `apst`.`Tid` , `apst`.`RoomIndex`
The mapping
<?xml version="1.0"?>
<hibernate-mapping>
<class name="com.abc.def.entity.ReservationTransactionView"
table="Reservation_Transaction_view" catalog="abcd">
<id name="id" type="string" />
<property name="transactionId" type="java.lang.Integer">
<column name="transactionId" />
</property>
<property name="roomNo" type="java.lang.Integer">
<column name="`room no`" />
</property>
<property name="paymentId" type="java.lang.Integer">
<column name="paymentId" />
</property>
<property name="reservationId" type="java.lang.Integer">
<column name="reservationId" />
</property>
The error is as followed.
org.hibernate.HibernateException: Missing table: Reservation_Transaction_view
I am using MySQL and Hibernate3.0 Please Help me for this query.
Thanks in advance.
Related
I'm trying to insert a new register but when Hibernate creates the insert it doesn't put the FK value.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ar.com.grupo1.entity.Turno" table="Turno">
<id name="numeroTurno" type="int">
<column name="Id_Turno" />
<generator class="identity" />
</id>
<property name="nombrePaciente" type="string">
<column name="Nombre" length="60" not-null="true" />
</property>
<property name="apellidoPaciente" type="string">
<column name="Apellido" length="60" not-null="true" />
</property>
<property name="planMedico" type="string">
<column name="PlanMedico" length="30" not-null="true" />
</property>
<property name="fechaLlegada" type="string">
<column name="FechaHoraLlegada" length="60" />
</property>
<property name="fechaAtencion" type="string">
<column name="FechaHoraAtencion" length="60" />
</property>
<property name="importe" type="float">
<column name="Importe" />
</property>
<property name="observaciones" type="string">
<column name="Observaciones" length="250" />
</property>
<one-to-one name="prestador" class="ar.com.grupo1.entity.Prestador"></one-to-one>
<one-to-one name="practica" class="ar.com.grupo1.entity.Practica"></one-to-one>
<one-to-one name="socio" class="ar.com.grupo1.entity.Socio" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
On the insert is there isn't the Id_Socio value.
INFO [STDOUT] Hibernate: insert into Turno (Nombre,
Apellido, PlanMedico, FechaHoraLlegada, FechaHoraAtencion, Importe,
Observaciones) values (?, ?, ?, ?, ?, ?, ?)
WARN [JDBCExceptionReporter] SQL Error: 515, SQLState: 23000
ERROR [JDBCExceptionReporter] Værdien NULL kan ikke indsættes i
kolonnen 'Id_Socio', tabellen 'grupo1.dbo.Turno'. Kolonnen tillader
ikke null-værdier. INSERT kan ikke udføres.
UPDATE
I've already checked that the Socio's, Practica's, and Prestador's ID exists in the database.
I'm giving to Turnos all these classes as attributes, it gets them correctly but at the time it does the Insert operation, is not inserting the FK values.
I have two tables: patient_data and patient_diagnosis
Patient_data contains personal data of patient like: pid (pkey), gender, birth_date
patient_diagnosis contains the diagnosis data of the registered patients. It has fields like: record_id (pkey), pid (fkey to patient_data(pid)), diagnosis_date and other related fields.
Now, I want to join these two tables on pid and have all these fields in a single type of object.
Here is the mapping file:
<hibernate-mapping>
<class catalog="emr" name="in.Models.Emr" table="patient_diagnosis">
<id name="recordid" type="long">
<column name="record_id"/>
</id>
<property name="diagnosisDate" type="timestamp">
<column length="19" name="diagnosis_date" not-null="true"/>
</property>
<property name="snomedTermPrimary" type="long">
<column name="snomed_term_primary" not-null="true" />
</property>
<property name="snomedTermSecondary" type="string">
<column name="snomed_term_secondary" />
</property>
<property name="episodeNo" type="long">
<column name="episode_no" not-null="true" />
</property>
<property name="pid" type="long">
<column name="pid" not-null="true" />
</property>
<join table="patient_data">
<key column="pid"/>
<property name="gender" type="string">
<column name="gender" not-null="true"/>
</property>
<property name="birthDate" type="timestamp">
<column length="19" name="birth_date" not-null="true"/>
</property>
</join>
</class>
</hibernate-mapping>
But, the join applies on patient_diagnosis.record_id = patient_data.pid instead of patient_diagnosis.pid = patient_data.pid i.e. HQL applies on primary key of first table with mentioned column from second table.
Please provide the solution so that join can be applied on mentioned column from first with mentioned column from second table. Or is there another way out?
Please note that in case I didn't create classes for patient_data or patient_diagnosis. But, just Emr class having combination of fields of these tables is created.
Try giving foreign key
<id name="pid" type="java.lang.Long">
<column name="pid" />
<generator class="foreign">
<param name="property">patient_data</param>
</generator>
</id>
I am not sure but maybe this should work.
And
<one-to-one name="patient_data" class="in.Models.Emr"
cascade="save-update"></one-to-one>
Similarly in Join class
<one-to-one name="patient_diagnosis" class="in.Models.Emr"
cascade="save-update"></one-to-one>
I hope this helps you.
I have a poor time performance in a SqlQuery under Hibernate 3.6.9 Final with a DB2 database. The sql is made quickly from the SQL Client (Squirrel):
Query 1 of 1, Rows read: 175, Elapsed time (seconds) - Total: 0.527, SQL query: 0.233, Reading results: 0.29
I'm using a ResultTransformer of my own, but it works fine and quickly (I have measured times).
I have been looking the times all around and I found that all the time (about 12 seconds!!!) is lost when the org.hibernate.loader.Loader is making some calls to its methods:
11:21:17,026 DEBUG 127.0.0.1 - MYAPPWEB/api/requests/search - org.hibernate.jdbc.AbstractBatcher[426] - about to open ResultSet (open ResultSets: 0, globally: 0)
11:21:17,205 DEBUG 127.0.0.1 - MYAPPWEB/api/requests/search - org.hibernate.loader.Loader[1322] - result row:
11:21:17,272 DEBUG 127.0.0.1 - /MYAPPWEB/api/requests/search - org.hibernate.loader.Loader[1322] - result row:
...175 rows all the same...
11:21:27,978 DEBUG 127.0.0.1 - /MYAPPWEB/api/requests/search - org.hibernate.loader.Loader[1322] - result row:
11:21:28,037 DEBUG 127.0.0.1 - /MYAPPWEB/api/requests/search - org.hibernate.loader.Loader[1322] - result row:
11:21:28,096 DEBUG 127.0.0.1 - /MYAPPWEB/api/requests/search - org.hibernate.jdbc.AbstractBatcher[433] - about to close ResultSet (open ResultSets: 1, globally: 1)
11:21:28,097 DEBUG 127.0.0.1 - /MYAPPWEB/api/requests/search - org.hibernate.jdbc.AbstractBatcher[418] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
As you can see here at the logs on the left, about 11 seconds are spent. I googled on this Hibernate Class and found the source code here: http://goo.gl/6oAXLT and came to the conclussion that the getRow() method from the Loader is making 1 query per row (total: 175 times) for cheking the ids and so on, but I don't know how to disable this behaviour or which is the configuration I should modify for avoiding this delay.
My mappings are:
Main object (Request):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.myappweb.model.requests.request.Request"
table="REQUEST" schema="${db2.app.schema}">
<id name="id" type="java.lang.Long" access="field">
<column name="REQUEST_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" not-null="true" />
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="true" />
</property>
<property name="improvement" type="java.lang.String">
<column name="BENEFIT" not-null="true" />
</property>
<property name="startScheduledDate" type="java.util.Date">
<column name="START_SCHEDULED_DATE" not-null="false" />
</property>
<property name="createTimestamp" type="java.sql.Timestamp">
<column name="CREATE_DATE" not-null="true" />
</property>
<property name="createUser" type="java.lang.String">
<column name="CREATE_USER" not-null="true" />
</property>
</class>
<!-- I omit the named SQL queries here -->
</hibernate-mapping>
Lac:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.myappweb.model.requests.lac.Lac"
table="REQUEST_ENTERPRISE_AREA" schema="${db2.app.schema}">
<composite-id name="id"
class="com.enterprise.myappweb.model.requests.lac.LacId">
<key-property name="requestId" type="java.lang.Long">
<column name="REQUEST_ID" not-null="true" />
</key-property>
<key-property name="lacId" type="java.lang.Integer">
<column name="ENTERPRISE_AREA_ID" not-null="true" />
</key-property>
</composite-id>
<property name="order" type="java.lang.Integer">
<column name="ORDER" not-null="true" />
</property>
<property name="startScheduledDate" type="java.util.Date">
<column name="START_SCHEDULED_DATE" not-null="false" />
</property>
<property name="finalScheduledDate" type="java.util.Date">
<column name="END_SCHEDULED_DATE" not-null="false" />
</property>
</class>
<!-- I omit the named SQL queries here -->
</hibernate-mapping>
PriorityType:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.myappweb.model.requests.type.PriorityType"
table="PRIORITY_TYPE_REQUEST" schema="${db2.app.schema}">
<id name="id" type="java.lang.Short" access="field">
<column name="PRIORITY_TYPE_REQUEST_ID" />
<generator class="native" />
</id>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="true" />
</property>
</class>
<!-- I omit the named SQL queries here -->
</hibernate-mapping>
Status:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.myappweb.model.requests.type.Status"
table="REQUEST_STATUS" schema="${db2.app.schema}">
<id name="id" type="java.lang.Short" access="field">
<column name="REQUEST_STATUS_ID" />
<generator class="native" />
</id>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="true" />
</property>
</class>
<!-- I omit the named SQL queries here -->
</hibernate-mapping>
RequestType:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.myappweb.model.requests.type.RequestType"
table="REQUEST_TYPE_REQUEST" schema="${db2.app.schema}">
<id name="id" type="java.lang.Short" access="field">
<column name="REQUEST_TYPE_REQUEST_ID" />
<generator class="native" />
</id>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" not-null="true" />
</property>
</class>
<!-- I omit the named SQL queries here -->
</hibernate-mapping>
Java code:
#Repository("requestDao")
public class RequestDaoHibernate extends AbstractReadWriteDao<Request, Long> implements RequestDao {
#Override
public List<Request> findRequests(final RequestSearchDto requestSearchDto, final Boolean isWarehouseUser) {
queryString = "SELECT .... FROM ...."; // Long string
List<Request> result = this.listSQLQuery(queryString.toString(), new SQLQueryCallback() {
#Override
public void doWithQuery(final SQLQuery query) {
// Recuperamos idiomas (del usuario y por defecto) a través de spring
String idiom = LocaleContextHolder.getLocale().getLanguage().toUpperCase();
query.setParameter("isoCodeLanguageId", 3);
query.setParameter("defaultLanguageId", 1);
for(Entry<String, Object> e : dictionary.entrySet()) {
if(e.getKey().endsWith("List")) {
query.setParameterList(e.getKey(), (Collection)e.getValue());
} else {
query.setParameter(e.getKey(), e.getValue());
}
}
RequestSearchRT rt = new RequestSearchRT();
query.setResultTransformer(rt);
}});
return result;
}
}
SuppressWarnings({"unchecked"})
public <S> List<S> listSQLQuery(final String sqlQuery,
final SQLQueryCallback callback) {
if (LOG.isDebugEnabled()) {
LOG.debug("Listing elements with SQL query: {}", sqlQuery);
}
return (List<S>) getHibernateTemplate().execute(new HibernateCallback() {
#Override
public List<S> doInHibernate(final Session session) throws SQLException {
SQLQuery query = session.createSQLQuery(sqlQuery);
callback.doWithQuery(query);
return query.list();
}
});
}
}
I will update the post with configuration settings as people ask me, because I think all the config is in several files and are big.
Thanks in advance.
Please have a look at the following
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.att_marks.students.Attendance_Btech">
<id name="id" type="integer" column="id" >
<generator class="increment"/>
</id>
<property name="year">
<column name="Year" />
</property>
<property name="semister">
<column name="Semister"/>
</property>
<property name="section">
<column name="Section"/>
</property>
<property name="period">
<column name="Period"/>
</property>
<property name="subjectCode">
<column name="Subject_Code"/>
</property>
<property name="date">
<column name="Date"/>
</property>
<property name="status">
<column name="Status"/>
</property>
<union-subclass name="com.att_marks.students.Attendance_Btech_ECE" table="attendance_btech_ece">
</union-subclass>
</class>
</hibernate-mapping>
I have tables named attendance_btech_ece, attendance_btech_cse, attendance_btech_it, and so on. All these tables use the same schema as mentioned above for the Attendance_Btech class. I thought this should work but it isn't.
If the Attendance_Btech_ECE is subclass of Attendance_Btech then should work.
I'm building a data warehouse and want to use InfiniDB as the storage engine. However, it doesn't allow primary keys or foreign key constraints (or any constraints for that matter).
Hibernate complains "The database returned no natively generated identity value" when I perform an insert.
Each table is relational, and contains a unique integer column that was previously used as the primary key - I want to keep that, but just not have the constraint in the db that the column is the primary key.
I'm assuming the problem is that Hibernate expects the db to return a generated key. Is it possible to override this behaviour so I can set the primary key field's value myself and keep Hibernate happy?
-- edit --
Two of the mappings are as follows:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visitor" table="visitor" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstSeen" type="timestamp">
<column name="first_seen" length="19" />
</property>
<property name="lastSeen" type="timestamp">
<column name="last_seen" length="19" />
</property>
<property name="sessionId" type="string">
<column name="session_id" length="26" unique="true" />
</property>
<property name="userId" type="java.lang.Long">
<column name="user_id" />
</property>
<set name="visits" inverse="true">
<key>
<column name="visitor_id" />
</key>
<one-to-many class="com.example.project.Visit" />
</set>
</class>
</hibernate-mapping>
and:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visit" table="visit" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="visitor" class="com.example.project.Visitor" fetch="join" cascade="all">
<column name="visitor_id" />
</many-to-one>
<property name="visitId" type="string">
<column name="visit_id" length="20" unique="true" />
</property>
<property name="startTime" type="timestamp">
<column name="start_time" length="19" />
</property>
<property name="endTime" type="timestamp">
<column name="end_time" length="19" />
</property>
<property name="userAgent" type="string">
<column name="user_agent" length="65535" />
</property>
<set name="pageViews" inverse="true">
<key>
<column name="visit_id" />
</key>
<one-to-many class="com.example.project.PageView" />
</set>
</class>
</hibernate-mapping>
As you noted in a comment, there're many id generators you can use. E.g., you many find 'increment' convenient. Complete overview
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id
And hibernate won't care about your db restrictions (both primary keys and foreign keys). In fact, hibernate has no way of knowing about certain db restriction until it's violated. And if restriction doesn't exist, it can never be violated :)
You can remove the <generator class="identity" /> element and set the id manually before saving the object.