Почему hibernate загружает все классы в теге отображения в Hibernate.cgf. xml? - PullRequest
1 голос
/ 08 марта 2020
Need help?
In my main class i m creating object of only UserDetails class and trying to save data in table.
But hibernate is trying to drop and create all tables mentioned in mapping tag in hibernate.cfg.xml.

Can anyone explain why?

I have attached all the relevant screenshots,Please help where i am going wrong.

ошибка не возникает, если я сохраняю только отображение классов Userdeatils в hibernate.cfg. xml

My main class is :
here i am creating objects of UserDetails class
public class Main {


public static void main(String[] args) {
    UserDetails ud= new UserDetails();
    UserDetails ud1= new UserDetails();
    UserDetails ud2= new UserDetails();
    UserDetails ud3= new UserDetails();
    ud.setUserName("Manav");

    ud1.setUserName("Gupta");
    ud2.setUserName("Manav1");
    ud3.setUserName("Gupta1");

    StandardServiceRegistry standardRegistry = 
               new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata metaData =    new MetadataSources(standardRegistry).getMetadataBuilder().build();
    SessionFactory  sessionFactory = metaData.getSessionFactoryBuilder().build();
     Session session =sessionFactory.openSession();
     Transaction transaction = session.beginTransaction();
     session.save(ud);
     session.save(ud1);
     session.save(ud2);
     session.save(ud3);
     transaction.commit();
     session.close();   }


UserDeatils class is :

@Entity(name="User_Details")
public class UserDetails {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)//--if auto generated
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }


}

Теперь в hibernate.cfg. xml у меня есть и другие классы, они также подтягиваются. Hibernate.cgf. xml is:

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle12cDialect</property>  
       <!-- <property name="hibernate.dialect"> hibernate.dialect net.sf.hibernate.dialect.Oracle10gDialect</property>-->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url"></property>
        <property name="hibernate.cache.region_prefix">hibernate.test</property>
        <property name="hibernate.jdbc.use_streams_for_binary">true</property>
        <property name="hibernate.jdbc.batch_size">0</property>
        <property name="hibernate.max_fetch_depth">3</property>
        <property name=" hibernate.cache.use_query_cache">true</property>          
         <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.generate_statistics">true</property>
        <property name="show_sql">true</property>   
        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> 


        <mapping class="com.myproj.BasicLearn.StudentInfoClass"/>
        <mapping class="com.myproj.BasicLearn.Vehicle"/>        
        <mapping class="com.myproj.BasicLearn.UserDetails"/>
           </session-factory>
</hibernate-configuration>

Console log is :

org.hibernate.tool.schema.spi.CommandAcceptanceException: Ошибка при выполнении DDL "создать таблицу Student_Info Причина: java. sql .SQLSyntaxErrorException: ORA-00955: имя уже используется существующим объектом, та же ошибка, которую я получаю для каждого класса Может кто-нибудь объяснить, почему это происходит?

  [1]: https://i.stack.imgur.com/Y7mdK.png
  [2]: https://i.stack.imgur.com/U68qI.png
  [3]: https://i.stack.imgur.com/pnxG8.jpg
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...