Witam,
jak najprościej napisać klasę odwzorowującą tabele
CREATE TABLE typy (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
typ ENUM('A','B','C') NOT NULL DEFAULT 'A'
);
dla Hibernate?
Witam,
jak najprościej napisać klasę odwzorowującą tabele
CREATE TABLE typy (
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
typ ENUM('A','B','C') NOT NULL DEFAULT 'A'
);
dla Hibernate?
enum Typy{
A, B, C;
}
Enum musi mieć unikalną wartość, a zatem nie potrzebujesz id.
Jeżeli tabela jest trochę większa tzn. jedna z kolumn jest typu enum:
class Klasa{
private MyEnum myEnum = MyEnum.A;
static enum MyEnum{A, B, C;}
// getery settery
}
OT
dlaczego nie potrzeba id moze byc (1,A)(2,A)(3,A)(4,A) ?! albo czegos nie zauwazylem ?
Jeżeli chodzi o tą tabele to jest to jedynie przykład.
Zrobiłem tak:
package test.model;
public enum TypyEnum {
A,
B,
C
}
package test.model;
import java.io.Serializable;
public class Typy implements Serializable {
private int id;
private TypyEnum typ = TypyEnum.A;
private Typy() {}
public Typy(TypyEnum typ) {
this.typ = typ;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public TypyEnum getTyp() {
return typ;
}
public void setTyp(TypyEnum typ) {
this.typ = typ;
}
}
Mapowanie:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="test.model">
<class name="Typy"
table="typy"
lazy="true">
<id name="id"
type="int"
column="id"
unsaved-value="null"
access="org.hibernate.property.DirectPropertyAccessor">
<generator class="increment"/>
</id>
<property name="typ"
type="test.model.TypyEnum"
column="typ"
not-null="true"/>
</class>
</hibernate-mapping>
i uruchamiam:
package test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.File;
import test.model.*;
public class Main {
private static SessionFactory factory;
static {
Configuration cfg = new Configuration();
cfg.configure(new File("../conf/hibernate.cfg.xml") );
factory = cfg.buildSessionFactory();
}
public Main() {
Typy t = new Typy(TypyEnum.B);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
session.close();
}
public static void main(String args[]) {
new Main();
}
}
Jednak wyskakuje mi błąd:
java.sql.BatchUpdateException: Data truncated for column 'typ' at row 1
Pełen StackTrace:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at test.Main.<init>(Main.java:31)
at test.Main.main(Main.java:36)
Caused by: java.sql.BatchUpdateException: Data truncated for column 'typ' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more
korzystam z MySQL 5,
podejrzewam, że ma to zwiazek ze zmienną i wartością STRICT_TRANS_TABLES
sql_mode
sql_mode | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Witam!
Hm.. może nie jest to idealne rozwiązanie, ale ja bym w tym przypadku nie używał MySQLowego enuma ale inta i mapował na Enuma w klasie
Działający przyklad można znaleźć na stronie:
http://weblog.dangertree.net/2007/09/23/mapping-java-5-enums-with-hibernate/
P. Wolanski