С нуля разбираюсь с хибером. Написал 3 класса: | Код |
@Entity @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING) public abstract class AbstractEntity implements Serializable {
@Id @GeneratedValue(strategy= GenerationType.AUTO) protected Long id; protected String model; protected String marka; protected int year; protected double engineVolume; protected double enginePower; protected double tankVolume; protected double cost; public Long getId() { return id; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getMarka() { return marka; } public void setMarka(String marka) { this.marka = marka; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public double getEngineVolume() { return engineVolume; } public void setEngineVolume(double engineVolume) { this.engineVolume = engineVolume; } public double getEnginePower() { return enginePower; } public void setEnginePower(double enginePower) { this.enginePower = enginePower; } public double getTankVolume() { return tankVolume; } public void setTankVolume(double tankVolume) { this.tankVolume = tankVolume; } public double getCost() { return cost; } public void setCost(double cost) { this.cost = cost; } }
|
| Код | @Entity @DiscriminatorValue("AUTO") public class Auto extends AbstractEntity {
private String type; }
|
| Код | @Entity @DiscriminatorValue("MOTO") public class Moto extends AbstractEntity { private String use; public String getUse() { return use; } public void setUse(String use) { this.use = use; } }
|
Дальше настройка хибера: | Код | <hibernate-configuration>
<session-factory>
<!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/automoto?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8</property> <property name="connection.username">foo</property> <property name="connection.password">bar</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property>
<mapping class="psu.yanukov.oop.core.domain.Auto"/> <mapping class="psu.yanukov.oop.core.domain.Moto"/>
</session-factory>
</hibernate-configuration>
|
Простой класс для теста: | Код | public class Test {
public static void main(String[] args) { // TODO Auto-generated method stub Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //Auto auto = new Auto(); //auto.setCost(12); //auto.setMarka("ford"); Moto moto = new Moto(); moto.setCost(12); moto.setEnginePower(13); moto.setEngineVolume(14); moto.setMarka("marka"); moto.setModel("model"); moto.setTankVolume(15); moto.setYear(1234); moto.setUse("use"); moto.setCost(15); //session.save(auto); session.save(moto); session.getTransaction().commit(); session.close(); }
}
|
Вылетает с ошибкой | Код | Hibernate: insert into AbstractEntity (cost, enginePower, engineVolume, marka, model, tankVolume, year) values (?, ?, ?, ?, ?, ?, ?) Hibernate: insert into Moto (use, id) values (?, ?) Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [psu.yanukov.oop.core.domain.Moto] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2454) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2854) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) at psu.yanukov.oop.core.Test.main(Test.java:32) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'use, id) values ('use', 1)' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.Util.getInstance(Util.java:384) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2409) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2437) ... 15 more
|
Если же раскоментить строчки для Auto, а для Moto закоментить, то все отработает на ура... так в чем разница в этих классах???
|