Sql error 0 sqlstate 42p01

Hibernate Community Forums

I am trying to get a very basic example from the book «Professional Hibernate» working. When I solve one problem, another pops up. But here is one I cannot figure out. I have seen several similar posts, but no real solutions or even ideas that point one in the correct direction.

I am using Hibernate 3 with NetBeans and a PostgreSQL 8.3 database on a Mac OS X platform.

I have ONE class called CD. I used NetBeans to generate the code, from the «Entity Class From a Database» wizard. It generated the requisite POJO fields and annotations for those fields. However, I don’t seem to get the annotations to work (which is another problem entirely).

When I attempt to add an instance of CD to the database, I receive the following exception and debug log (this includes the generated SQL to attempt to insert the CD instance).

Code:

java.sql.BatchUpdateException: Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.

……(there’s a bunch of stack trace info here)……..

Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.
12168 [AWT-EventQueue-0] WARN  org.hibernate.util.JDBCExceptionReporter  — SQL Error: 0, SQLState: null
12168 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter  — Batch entry 0 insert into CD (title, artist, purchasedate, cost, id) values (test, test, 2009-02-20 -05:00:00, 1.0, 5) was aborted.  Call getNextException to see the cause.
        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
12168 [AWT-EventQueue-0] WARN  org.hibernate.util.JDBCExceptionReporter  — SQL Error: 0, SQLState: 42P01
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
12168 [AWT-EventQueue-0] ERROR org.hibernate.util.JDBCExceptionReporter  — ERROR: relation «cd» does not exist
12168 [AWT-EventQueue-0] ERROR org.hibernate.event.def.AbstractFlushingEventListener  — Could not synchronize database state with session
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

I created the following Mapping Documents for the CD class:
CD.hbm.xml

Code:

<?xml version=»1.0″ encoding=»UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC «-//Hibernate/Hibernate Mapping DTD 3.0//EN» «http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd»>
<hibernate-mapping>
  <class name=»org.scawa.prohibernate.pojo.CD» table=»CD»>
      <id name=»id»
          type=»int»>
          <column name=»id»
                  sql-type=»integer»
                  not-null=»true»/>
          <generator class=»sequence»/>
      </id>
      <property name=»title» type=»text» />
      <property name=»artist» type=»text» />
      <property name=»purchasedate» type=»date» />
      <property name=»cost» type=»double» />
  </class>
</hibernate-mapping>

Here is the SQL generated to create the Table CD:

Code:

CREATE TABLE «CD»
(
  id integer NOT NULL,
  title text,
  artist text,
  purchasedate date,
  «cost» double precision,
  CONSTRAINT «CD_pkey» PRIMARY KEY (id)
)
WITH (OIDS=FALSE);

The code that is used in the save is as follows:

Code:

                CD cd = new CD();

                cd.setTitle(titleField.getText());
                cd.setArtist(titleField.getText());
                cd.setCost(new Double(costField.getText()));
                cd.setPurchasedate(new Date());

                listModel.addCD(cd);
                Session session = null;
                try{
                    session = sessionFactory.openSession();
                    session.save(cd);
                }catch(Exception e){
                    System.out.println(«Exception attemtping to Add CD: » + e.getMessage());

                }finally{
                    if(session != null && session.isOpen()){
                        session.flush();
                        session.close();
                    }
                }

                IDLabel.setText(«»);
                titleField.setText(«»);
                artistField.setText(«»);
                costField.setText(«»);

I know that the configuration file is correct and the mapping parses correctly and maps because those are problems I have worked through before. I, also know, that the hibernate_sequence is working because it increments each time I run the add, and the DEBUG log says it works. This means I am hitting the correct Schema and database (the postgres database and public schema).

Any possible help here?

Thanks in advance.

Stephen McConnell

«Boredom is a personal defect.»

— Lamar Stephens

  • Remove From My Forums
  • Question

  • public bool girisKontrol(string user, string pass)
            {string sunucu, port, kullaniciadi, sifre, veritabani;
                sunucu = "localhost";
                port = "5432";
                kullaniciadi = "postgres";
                sifre = "tellioglu";
                veritabani = "postgres";
                string baglantimetni = string.Format("Server ={0};Port ={1};User ID = {2};Password = {3};Database={4};", sunucu, port, kullaniciadi, sifre, veritabani);
                var baglanti = new NpgsqlConnection();
                baglanti.ConnectionString = baglantimetni;
                var cmd = new NpgsqlCommand();
                cmd.Connection = baglanti;
                cmd.CommandText = "select * from kullanicigiris where Kullaniciadi = @Kullanici and sifre = @sifre";//kullanicigiris tablonun adi , Kullaniciadi sütünun adı,sifre sütunun adi
                cmd.Parameters.AddWithValue("@Kullanici", user);
                cmd.Parameters.AddWithValue("@sifre", pass);
                cmd.Connection.Open();
                var reader = cmd.ExecuteReader();
                var sonuc = reader.HasRows;
                reader.Close();
                reader.Dispose();
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
                return sonuc;
    
            }

    i am using postgreSQL database . executereader(); giving ‘ERROR: 42P01: relation does not exist’ problem. is sql line wrong i dont know please help me

    
    

Answers

  • From the code sinppet, I don’t think it’s the SQL line error. In C#, we should use the SQL parameters like yours.

    But for postgreSQL, I would suggest you to try the following code to see if it works.

    cmd.CommandText = "select * from kullanicigiris where Kullaniciadi = :Kullanici and sifre = :sifre"     
           cmd.Parameters.AddWithValue(":Kullanici", user);
                cmd.Parameters.AddWithValue(":sifre", pass);
    

    And there is a category for postgreSQL support query for your reference:

    http://www.postgresql.org/support/

    Hope it hleps.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by

      Wednesday, May 2, 2012 2:51 AM

Thank you @luceos for your support. I have tried to include every details from the project. I hope this will help you to identify my issue.

Laravel: 5.7.*
Hyn multi-tenant: 5.3.*

Composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "hyn/multi-tenant": "5.3.*",
        "laravel/framework": "5.7.*",
        "laravel/tinker": "^1.0",
        "owen-it/laravel-auditing": "^8.0",
        "prettus/l5-repository": "^2.6",
        "rap2hpoutre/laravel-log-viewer": "^0.22.1",
        "spatie/laravel-permission": "^2.28"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r "file_exists('.env') || copy('.env.example', '.env');""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

config/tenancy.php

<?php

/*
 * This file is part of the hyn/multi-tenant package.
 *
 * (c) Daniël Klabbers <daniel@klabbers.email>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see https://laravel-tenancy.com
 * @see https://github.com/hyn/multi-tenant
 */

use HynTenancyDatabaseConnection;
use HynTenancyGeneratorsDatabaseDefaultPasswordGenerator;
use HynTenancyGeneratorsUuidShaGenerator;
use HynTenancyMiddlewareEagerIdentification;
use HynTenancyMiddlewareHostnameActions;
use HynTenancyModelsHostname;
use HynTenancyModelsWebsite;

return [
    'models'     => [
        /**
         * Specify different models to be used for the global, system database
         * connection. These are also used in their relationships. Models
         * used have to implement their respective contracts and
         * either extend the SystemModel or use the trait
         * UsesSystemConnection.
         */

        // Must implement HynTenancyContractsHostname
        'hostname' => Hostname::class,

        // Must implement HynTenancyContractsWebsite
        'website'  => Website::class,
    ],
    /**
     * The package middleware. Removing a middleware here will disable it.
     * You can of course extend/replace them or add your own.
     */
    'middleware' => [
        // The eager identification middleware.
        EagerIdentification::class,

        // The hostname actions middleware (redirects, https, maintenance).
        HostnameActions::class,
    ],
    'website'    => [
        /**
         * Each website has a short random hash that identifies this entity
         * to the application. By default this id is randomized and fully
         * auto-generated. In case you want to force your own logic for
         * when you need to have a better overview of the complete
         * tenant folder structure, disable this and implement
         * your own id generation logic.
         */
        'disable-random-id'            => false,

        /**
         * The random Id generator is responsible for creating the hash as mentioned
         * above. You can override what generator to use by modifying this value
         * in the configuration.
         *
         * @warn This won't work if disable-random-id is true.
         */
        'random-id-generator'          => ShaGenerator::class,

        /**
         * Enable this flag in case you're using a driver that does not support
         * database username or database name with a length of more than 32 characters.
         *
         * This should be enabled for MySQL, but not for MariaDB and PostgreSQL.
         */
        'uuid-limit-length-to-32'      => env('LIMIT_UUID_LENGTH_32', false),

        /**
         * Specify the disk you configured in the filesystems.php file where to store
         * the tenant specific files, including media, packages, routes and other
         * files for this particular website.
         *
         * @info If not set, will revert to the default filesystem.
         * @info If set to false will disable all tenant specific filesystem auto magic
         *       like the config, vendor overrides.
         */
        'disk'                         => null,

        /**
         * Automatically generate a tenant directory based on the random id of the
         * website. Uses the above disk to store files to override system-wide
         * files.
         *
         * @info set to false to disable.
         */
        'auto-create-tenant-directory' => true,

        /**
         * Automatically rename the tenant directory when the random id of the
         * website changes. This should not be too common, but in case it happens
         * we automatically want to move files accordingly.
         *
         * @info set to false to disable.
         */
        'auto-rename-tenant-directory' => true,

        /**
         * Automatically deletes the tenant specific directory and all files
         * contained within.
         *
         * @see
         * @info set to true to enable.
         */
        'auto-delete-tenant-directory' => env('TENANCY_DIRECTORY_AUTO_DELETE', false),

        /**
         * Time to cache websites in minutes. Set to false to disable.
         */
        'cache'                        => 10,
    ],
    'hostname'   => [
        /**
         * If you want the multi tenant application to fall back to a default
         * hostname/website in case the requested hostname was not found
         * in the database, complete in detail the default hostname.
         *
         * @warn this must be a FQDN, these have no protocol or path!
         */
        'default'                           => env('TENANCY_DEFAULT_HOSTNAME'),
        /**
         * The package is able to identify the requested hostname by itself,
         * disable to get full control (and responsibility) over hostname
         * identification. The hostname identification is needed to
         * set a specific website as currently active.
         *
         * @see src/Jobs/HostnameIdentification.php
         */
        'auto-identification'               => env('TENANCY_AUTO_HOSTNAME_IDENTIFICATION', true),

        /**
         * In case you want to have the tenancy environment set up early,
         * enable this flag. This will run the tenant identification
         * inside a middleware. This will eager load tenancy.
         *
         * A good use case is when you have set "tenant" as the default
         * database connection.
         */
        'early-identification'              => env('TENANCY_EARLY_IDENTIFICATION', true),

        /**
         * Abort application execution in case no hostname was identified. This will throw a
         * 404 not found in case the tenant hostname was not resolved.
         */
        'abort-without-identified-hostname' => env('TENANCY_ABORT_WITHOUT_HOSTNAME', false),

        /**
         * Time to cache hostnames in minutes. Set to false to disable.
         */
        'cache'                             => 10,

        /**
         * Automatically update the app.url configured inside Laravel to match
         * the tenant FQDN whenever a hostname/tenant was identified.
         *
         * This will resolve issues with password reset mails etc using the
         * correct domain.
         */
        'update-app-url'                    => false,
    ],
    'db'         => [
        /**
         * The default connection to use; this overrules the Laravel database.default
         * configuration setting. In Laravel this is normally configured to 'mysql'.
         * You can set a environment variable to override the default database
         * connection to - for instance - the tenant connection 'tenant'.
         */
        'default'                           => env('TENANCY_DEFAULT_CONNECTION', Connection::DEFAULT_TENANT_NAME),
        /**
         * Used to give names to the system and tenant database connections. By
         * default we configure 'system' and 'tenant'. The tenant connection
         * is set up automatically by this package.
         *
         * @see src/Database/Connection.php
         * @var system-connection-name The database connection name to use for the global/system database.
         * @var tenant-connection-name The database connection name to use for the tenant database.
         */
        'system-connection-name'            => env('TENANCY_SYSTEM_CONNECTION_NAME', Connection::DEFAULT_SYSTEM_NAME),
        'tenant-connection-name'            => env('TENANCY_TENANT_CONNECTION_NAME', Connection::DEFAULT_TENANT_NAME),

        /**
         * The tenant division mode specifies to what database websites will be
         * connecting. The default setup is to use a new database per tenant.
         * If using PostgreSQL, a new schema per tenant in the same database can
         * be setup, by optionally setting division mode to 'schema'.
         * In case you prefer to use the same database with a table prefix,
         * set the mode to 'prefix'.
         * To implement a custom division mode, set this to 'bypass'.
         *
         * @see src/Database/Connection.php
         */
        'tenant-division-mode'              => env('TENANCY_DATABASE_DIVISION_MODE', 'database'),

        /**
         * The database password generator takes care of creating a valid hashed
         * string used for tenants to connect to the specific database. Do
         * note that this will only work in 'division modes' that set up
         * a connection to a separate database.
         */
        'password-generator'                => DefaultPasswordGenerator::class,

        /**
         * The tenant migrations to be run during creation of a tenant. Specify a directory
         * to run the migrations from. If specified these migrations will be executed
         * whenever a new tenant is created.
         *
         * @info set to false to disable auto migrating.
         *
         * @warn this has to be an absolute path, feel free to use helper methods like
         * base_path() or database_path() to set this up.
         */
        'tenant-migrations-path'            => database_path('migrations/tenant'),

        /**
         * The default Seeder class used on newly created databases and while
         * running artisan commands that fire seeding.
         *
         * @info requires tenant-migrations-path in order to seed newly created websites.
         * @info seeds stored in `database/seeds/tenants` need to be configured in your composer.json classmap.
         *
         * @warn specify a valid fully qualified class name.
         */
        'tenant-seed-class'                 => TenancyDatabaseSeeder::class,
        //      eg an admin seeder under `app/Seeders/AdminSeeder.php`:
        //        'tenant-seed-class' => AppSeedersAdminSeeder::class,

        /**
         * Automatically generate a tenant database based on the random id of the
         * website.
         *
         * @info set to false to disable.
         */
        'auto-create-tenant-database'      => true,

        /**
         * Automatically generate the user needed to access the database.
         *
         * @info Useful in case you use root or another predefined user to access the
         *       tenant database.
         * @info Only creates in case tenant databases are set to be created.
         *
         * @info set to false to disable.
         */
        'auto-create-tenant-database-user' => true,

        /**
         * Automatically rename the tenant database when the random id of the
         * website changes. This should not be too common, but in case it happens
         * we automatically want to move databases accordingly.
         *
         * @info set to false to disable.
         */
        'auto-rename-tenant-database'      => true,

        /**
         * Automatically deletes the tenant specific database and all data
         * contained within.
         *
         * @info set to true to enable.
         */
        'auto-delete-tenant-database'       => env('TENANCY_DATABASE_AUTO_DELETE', false),

        /**
         * Automatically delete the user needed to access the tenant database.
         *
         * @info Set to false to disable.
         * @info Only deletes in case tenant database is set to be deleted.
         */
        'auto-delete-tenant-database-user'  => env('TENANCY_DATABASE_AUTO_DELETE_USER', false),

        /**
         * Define a list of classes that you wish to force onto the tenant or system connection.
         * The connection will be forced when the Model has booted.
         *
         * @info Useful for overriding the connection of third party packages.
         */
        'force-tenant-connection-of-models' => [
        ],
        'force-system-connection-of-models' => [
        ],
    ],

    /**
     * Global tenant specific routes.
     * Making it easier to distinguish between landing and tenant routing.
     *
     * @info only works with `tenancy.hostname.auto-identification` or identification happening
     *       before the application is booted (eg inside middleware or the register method of
     *       service providers).
     */
    'routes'     => [
        /**
         * Routes file to load whenever a tenant was identified.
         *
         * @info Set to false or null to disable.
         */
        'path'           => base_path('routes/tenants.php'),

        /**
         * Set to true to flush all global routes before setting the routes from the
         * tenants.php routes file.
         */
        'replace-global' => true,
    ],

    /**
     * Folders configuration specific per tenant.
     * The following section relates to configuration to files inside the tenancy/<uuid>
     * tenant directory.
     */
    'folders'    => [
        'config' => [
            /**
             * Merge configuration files from the config directory
             * inside the tenant directory with the global configuration files.
             */
            'enabled'   => true,

            /**
             * List of configuration files to ignore, preventing override of crucial
             * application configurations.
             */
            'blacklist' => [ 'database', 'tenancy', 'webserver' ],
        ],
        'routes' => [
            /**
             * Allows adding and overriding URL routes inside the tenant directory.
             */
            'enabled' => true,

            /**
             * Prefix all tenant routes.
             */
            'prefix'  => null,
        ],
        'trans'  => [
            /**
             * Allows reading translation files from a trans directory inside
             * the tenant directory.
             */
            'enabled'         => true,

            /**
             * Will override the global translations with the tenant translations.
             * This is done by overriding the laravel default translator with the new path.
             */
            'override-global' => true,

            /**
             * In case you disabled global override, specify a namespace here to load the
             * tenant translation files with.
             */
            'namespace'       => 'tenant',
        ],
        'vendor' => [
            /**
             * Allows using a custom vendor (composer driven) folder inside
             * the tenant directory.
             */
            'enabled' => true,
        ],
        'media'  => [
            /**
             * Mounts the assets directory with (static) files for public use.
             */
            'enabled' => true,
        ],
        'views'  => [
            /**
             * Enables reading views from tenant directories.
             */
            'enabled'         => true,

            /**
             * Specify a namespace to use with which to load the views.
             *
             * @eg   setting `tenant` will allow you to use `tenant::some.blade.php`
             * @info set to null to add to the global namespace.
             */
            'namespace'       => null,

            /**
             * If `namespace` is set to null (thus using the global namespace)
             * make it override the global views. Disable by setting to false.
             */
            'override-global' => true,
        ],
    ],
];

debug.log

[2018-12-19 01:10:36] local.ERROR: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "hostnames" does not exist LINE 1: select * from "hostnames" where "fqdn" = $1 and "hostnames"....                       ^ (SQL: select * from "hostnames" where "fqdn" = localhost and "hostnames"."deleted_at" is null limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 42P01): SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "hostnames" does not existnLINE 1: select * from "hostnames" where "fqdn" = $1 and "hostnames"....n                      ^ (SQL: select * from "hostnames" where "fqdn" = localhost and "hostnames"."deleted_at" is null limit 1) at /Users/pusparaj/sources/andatech/cms/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42P01): SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "hostnames" does not existnLINE 1: select * from "hostnames" where "fqdn" = $1 and "hostnames"....n                      ^ at /Users/pusparaj/sources/andatech/cms/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119, PDOException(code: 42P01): SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "hostnames" does not existnLINE 1: select * from "hostnames" where "fqdn" = $1 and "hostnames"....n                      ^ at /Users/pusparaj/sources/andatech/cms/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117)"} []

Понравилась статья? Поделить с друзьями:
  • Sql error 0 no process is on the other end of the pipe
  • Sql developer oracle error
  • Sql developer error network adapter could not establish the connection
  • Sql developer error 17002
  • Sql count arithmetic overflow error converting expression to data type int